Solved

$.ajax failing to execute

Posted on 2013-01-16
15
270 Views
Last Modified: 2013-01-17
When I insert the following $.ajax method all the java scripts stop executing:

<script>

$( document ).ready(function() {

    $(".button").click(function() {

        var d=$("input#datepicker").val();
        var a=$("select#fldWithdrawingAccount").val();
        alert ("fldate=" + d + "&fldWithdrawingAccount=" + a);

        $.ajax({

            type:"POST"
            url : "../newTransaction.php",
            data: datastring

            });

        return false;
    });

});

</script>

Open in new window


The alerts work when I comment out the $.ajax.
The php codes works when called by the form POST method in html.

I am using the following jQuery libraries

<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" type="text/css">
  <link rel="stylesheet" href="/resources/demos/style.css" type="text/css">
  <link rel="stylesheet" href="themes/jquery-ui-1.9.2.custom/jquery-ui-1.9.2.custom/css/custom-theme/jquery-ui-1.9.2.custom.css" type="text/css">
  <script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
  <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"></script>
  <script type="text/javascript">
  $(function()

        {
            $("#datepicker").datepicker({dateFormat: "dd M yy", changeMonth: true, changeYear: true});

            $("#datepicker").attr('readOnly', 'true');

        }
    );
  </script>

Open in new window


So what do it need to do to make the $.ajax execute? Am I missing a library or something?
0
Comment
Question by:Sheils
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38784618
you miss a comma line 13

type:"POST", //<----- here
0
 
LVL 16

Author Comment

by:Sheils
ID: 38784770
Thanks Leakim,

Can't believe it took me 3 days and I could not see the missing comma. The javascripts are working now but the php is still not executing. The PHP code and form code are as follows:

php file

<?php

if (isset ($_POST['send'])) {
  $Date = $_POST['flddate'];
   $WithdrawingAccount = $_POST['fldWithdrawingAccount'];
  $sql = "INSERT INTO `tbTransactions` (`fldDate`, `fldWithdrawingAccount`) VALUES (STR_TO_DATE('" . $Date . "', '%d %b %Y')," . $WithdrawingAccount . ")";
  mysqli_query($cnn, $sql) or die(mysqli_error($cnn));

}

?>

Open in new window


form

echo "<div id='newexpenses' class='mainformheader'>";
 	echo "<form class='mainform' method='' action=''>";
 	"<input type='hidden' name='navbutton' value='NewExpenses'>";
 	echo "<div>Date:</div><input name='flddate'  id='datepicker'><br/>";
 	echo "<div>Type:</div><input id='fldtype' name='fldType'><br/>";
 	include "scripts/accountoptions.php";
 	echo "<div>Withdrawing Account:</div><select id='fldWithdrawingAccount' name='fldWithdrawingAccount'>" . $AccountsOption . "</select><br/>";
 	//echo "<div>Sales Person:</div><input value='".mysqli_result($result, $j-1,3)."' ><br/>";
 	echo "<input type='submit' name='submit2' class='button' id='submit_btn' value='Send' />";
 	echo "</div>";

Open in new window


scripts/accountoptions.php

if (!$result)
	{
		$currentaccount = 999;
	}
	else
	{
		$currentaccount = mysqli_result($result, $j - 1, 2);
	}
	include "queries/qryAccounts.php";
	$Accounts = mysqli_query($cnn, $Accountssql);
	$AccountsOption = $AccountsOption . "<option VALUE=''>" . $AccountsRow[1] . "</option>";
	while ($AccountsRow = mysqli_fetch_array($Accounts))
	{
		if ($AccountsRow[0] == $currentaccount)
		{
			$selected = "selected";
		}
		else
		{
			$selected = "";
		}
		$AccountsOption = $AccountsOption . "<option " . $selected . " VALUE=" . $AccountsRow[0] . ">" . $AccountsRow[1] . "</option>";
	}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38784784
please provide a link to your page
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 82

Expert Comment

by:leakim971
ID: 38784950
line 15 of the javascript you've : data: datastring
it produce an error, break  the ajax call and submit the page instead broking the submit

where do come from the variable datastring?
0
 
LVL 16

Author Comment

by:Sheils
ID: 38786243
Sorry I made a mistake while cutting and paste. I have now fixed that and datastring is returning:-

 fldate=29 Nov 2013&fldWithdrawingAccount=6

The code is now:
<script>

$( document ).ready(function() {

    $(".button").click(function() {

        var d=$("input#datepicker").val();
        var a=$("select#fldWithdrawingAccount").val();
        datastring="fldate=" + d + "&fldWithdrawingAccount=" + a;

                document.getElementById('test').innerHTML = datastring;

        $.ajax({

            type:"POST",
            url : "../newTransaction.php",
            data: datastring,

            success: function() {
                alert ("Gatch Ya")
            },

            error : function() {
                alert("Sorry, there was a problem!");

            },

            complete : function() {
                alert("The request is complete!");
            }
        });

        return false;
    });

});

</script>

Open in new window


I believe that the spaces in the date string may be the cause but I have not been able to make it work
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38786754
0
 
LVL 16

Author Comment

by:Sheils
ID: 38786799
That's my php file code as follows:

<?php

if (isset ($_POST['send'])) {
    $Date = $_POST['flddate'];
    $WithdrawingAccount = $_POST['fldWithdrawingAccount'];
    $sql = "INSERT INTO `tbTransactions` (`fldDate`, `fldWithdrawingAccount`) VALUES (STR_TO_DATE('" . $Date . "', '%d %b %Y')," . $WithdrawingAccount . ")";
    mysqli_query($cnn, $sql) or die(mysqli_error($cnn));

}

?>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38786834
perhaps but currently you've a 404 error, mean the name of the file is not good or the path to it is wrong
0
 
LVL 16

Author Comment

by:Sheils
ID: 38786838
Just noticed that should be POST http://wowislandcharter.com/wowaccounts/scripts/newTransaction.php

I changed the url: to

url : "../scripts/newTransaction.php",

still not working. What are you using to debug
0
 
LVL 16

Author Comment

by:Sheils
ID: 38786850
Both the javascript and the newTransaction.php are in the scripts folder. The index.php file is in the wowaccounts folder
But from what you have send it looks like it is looking  for newTransaction.php in the root folder.

So should it be:

url : "scripts/newTransaction.php"
0
 
LVL 16

Author Comment

by:Sheils
ID: 38786887
OK with this change it is going through and indicating that the ajax has been successful. However, the insert into does not execute because the new data is not in the database.

Can you find anything with this that will stop it from executing:


if (isset ($_POST['send'])) {
    $Date = $_POST['flddate'];
    $WithdrawingAccount = $_POST['fldWithdrawingAccount'];
    $sql = "INSERT INTO `tbTransactions` (`fldDate`, `fldWithdrawingAccount`) VALUES (STR_TO_DATE('" . $Date . "', '%d %b %Y')," . $WithdrawingAccount . ")";
    mysqli_query($cnn, $sql) or die(mysqli_error($cnn));

cheers
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38786925
look like you found it : scripts/newTransaction.php

full path : http://wowislandcharter.com/wowaccounts/scripts/newTransaction.php
0
 
LVL 16

Author Comment

by:Sheils
ID: 38787088
How do you want to progress from, here. Should I close this question and ask another question or can you find some simple fix for the query execution.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38787098
Yes, open a new question please, I did not saw your last post before now
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question