Solved

$.ajax failing to execute

Posted on 2013-01-16
15
267 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
LVL 16

Author Comment

by:Sheils
ID: 38784910
0
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
Help in good tutorials for PHP, HTML and CSS 6 40
Wordpress Security 29 48
Button on Table, name table1 not working 4 20
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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?
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 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