MateoIO
MateoIO

Reputation: 411

Jquery calling PHP

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

Upvotes: 0

Views: 80

Answers (2)

MateoIO
MateoIO

Reputation: 411

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.

Upvotes: 0

Xavier J
Xavier J

Reputation: 4624

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.

Upvotes: 1

Related Questions