$.ajax failing to execute

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?
LVL 16
SheilsAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
you miss a comma line 13

type:"POST", //<----- here
0
 
SheilsAuthor Commented:
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
 
leakim971PluritechnicianCommented:
please provide a link to your page
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
leakim971PluritechnicianCommented:
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
 
SheilsAuthor Commented:
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
 
leakim971PluritechnicianCommented:
0
 
SheilsAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
SheilsAuthor Commented:
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
 
SheilsAuthor Commented:
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
 
SheilsAuthor Commented:
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
 
leakim971PluritechnicianCommented:
look like you found it : scripts/newTransaction.php

full path : http://wowislandcharter.com/wowaccounts/scripts/newTransaction.php
0
 
SheilsAuthor Commented:
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
 
leakim971PluritechnicianCommented:
Yes, open a new question please, I did not saw your last post before now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.