Jquery调用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

我正在尝试从Jquery调用PHP文件。 单击我的按钮后,我的网站显示“hi”,因此我知道正在调用click()函数,但我没有得到test.php文件的响应。 此外,当我从独立于此脚本调用时,我的php文件加载时没有错误。 p>

 &lt; html&gt; 
&lt; head&gt; 
&lt; script language =“JavaScript”type =“  text / javascript“src =”jquery-2.1.4.js“&gt;&lt; / script&gt; 
&lt; script type =”text / javascript“&gt; 
 
 $(document).ready(function(){  
 $('#submitButton')。click(function(){
 // var theName = $(“#name”)。val(); 
 // var theID = $(“#transaction”)。  val(); 
 alert(“hi”); 
 $ .ajax({
 url:“test.php?name = Charlie&amp; transID = 1234”,
 type:'get',
 success:  function(data){
 alert(data); 
} 
}); 
} 
)
}); 
&lt; / script&gt; 
 
&lt; / head&gt; 
&lt; body&gt;  
&lt; form action =“”method =“POST”&gt; 
&lt; input id =“name”type =“text”size =“20”placeholder =“输入您的姓名”&gt; 
&lt;输入 id =“transaction”type =“hidden”name =“transactionID”value =“12345”&gt; 
&lt; input id =“submitButton”type =“submit”name =“subm 它“value =”调用PHP“&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
 
&lt; / html&gt; 
  code>  pre> 
 
 

PHP: p>

 &LT; PHP中
 $的名称= $ _ GET [ '名称']; 
 $的ID = $ _ GET [ 'TRANSID']; 
 \尼哥(” 谢谢$ name for transaction#:$ id“); 
?&gt; 
  code>  pre> 
 
 

谢谢,
Craig p> DIV>

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.

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 + '

' + errorThrown);
          }
       });

This will show any errors occurring during the ajax call.