0

I'm attempting to call a PHP file from Jquery. My website shows "hi" once my button is clicked so I know the click() function is being called but I don't get a response from my test.php file. Furthermore, my php file loads without error when called independent from this script.

<html>
<head>
<script language="JavaScript" type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('#submitButton').click(function(){
 //var theName = $( "#name" ).val();
 //var theID = $( "#transaction" ).val();
 alert("hi");
 $.ajax({
 url: "test.php?name=Charlie&transID=1234",
 type:'get',
 success: function(data){
 alert(data);
 }
 });
 }
 )
});
</script>
</head>
<body>
 <form action="" method="POST">
 <input id="name" type="text" size="20" placeholder="Enter Your Name">
 <input id="transaction" type="hidden" name="transactionID" value="12345">
 <input id="submitButton" type="submit" name="submit" value="Call PHP">
 </form>
</body>
</html>

PHP:

<?php
$name=$_GET['name'];
$id=$_GET['transID'];
echo("Thank you $name for transaction #:$id");
?>

Thanks,
Craig

asked Dec 29, 2015 at 16:21
9
  • 3
    Hi Craig, Could you please add test.php code too. So i can give to proper help. Commented Dec 29, 2015 at 16:23
  • 3
    Watch the network tab, what happens? Commented Dec 29, 2015 at 16:23
  • 1
    Is test.php in the same folder as the HTML file? Commented Dec 29, 2015 at 16:25
  • Either the OP ran off to school, work, other..., is busy trying to fix it themselves, or is expecting a magical answer; the latter is not going to happen. We won't setup a script just for you. You need to respond to the above comments, that's IF you're around. Commented Dec 29, 2015 at 16:42
  • 1
    Buy Stack stocks, like I did, you'll make a killing from helping people ;-) Commented Dec 29, 2015 at 21:00

2 Answers 2

1

It's possible that the ajax call to test.php page isn't working right. Change your ajax call code to:

 $.ajax({
 url: "test.php?name=Charlie&transID=1234",
 type:'get',
 success: function(data){
 alert(data);
 },
 error: function(jqXHR, textStatus, errorThrown){
 alert('Status:' + textStatus + '\r\n\r\n' + errorThrown);
 }
 });

This will show any errors occurring during the ajax call.

answered Dec 29, 2015 at 17:45
Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate this approach as its very methodical. For some reason it does not catch error 401 issue.
0

The solution was to turn off my password protected directory on my webserver which hosted these files in order to successfully call the php file using jquery. With the password protected directory turned on the jquery threw a Error 401 which I was able to catch by viewing the Network tab within Chrome. Still unsure why I was able to provide username/password once and continually access the files via my browser while my script was denied access during the same session.

answered Dec 30, 2015 at 11:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.