Solved

$.ajax failing to execute

Posted on 2013-01-16
15
264 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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 …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now