dongyong6332 2016-10-26 09:54
浏览 64
已采纳

使用ajax从Javascript函数调用Php函数

Hello everyone I am new to php. I have been trying out this thing when a user enter a product name need to validate that the product is valid or not. For that purpose I have used onchange event when the text is entered.The onchange function will call the javascript function.From javascript function I am calling the php which is in the same file.So when I am entering the product name somehow the php function is not working.

Here is my code :

<?php
  include 'conf.php';//it contains the php database configuration
   session_start();

   $quantityRequired=0; 
   $productName_error="";
    if(is_ajax()){
      if(isset($_POST["productName"])){
        $productName=$_POST["productName"];
        $row=mysqli_query($conn,"SELECT * from OrderDetails where ProductName='".$productName."'");
        if($row)
        {
          $result=mysqli_fetch_assoc($row);
          $quantityRequired=$result["Quantity"];
        }   
        else
         {
          $productName_error="The product name is not valid or product does not exist";
          echo $productName_error;
         }  
      }
    } 
  function is_ajax() {
    $flag=(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); 
    return $flag;
  }
?>
<html>
   <head>
       <title>Order Page </title>
       <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
   </head>
   <body>
      <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
         <label for="userName">Username</label><br>
         Product Name<input type="text" name="productName" id="productName" onchange="validateProduct()"><?php echo $productName_error?><br>
         Quantity Required<input type="text" name="quantityRequired" id="quantityRequired"><br>
         Availability<input type="text" name="availability">
         <p id="demo"></p>
      </form>
      <script>
        function validateProduct()
        {
            $.ajax({
             type: "POST"
         });

        }
      </script>
   </body>  
</html> 

so the code is when the user enters the product name.The function validate product is called.From validate product it will call the php which is in the same file. is_ajax() function is used to check whether it is the ajax request or not.

  • 写回答

3条回答 默认 最新

  • doujiang3997 2016-10-26 10:15
    关注

    There may be other problems I haven't spotted, but the first thing that jumps out to me is that your server-side code runs conditionally:

    if(isset($_POST["productName"]))
    

    And that condition was never satisfied because you didn't send any values in the AJAX request:

    $.ajax({
        type: "POST"
    });
    

    Send the value(s) you're looking for:

    $.ajax({
        type: "POST",
        data: { productName: $('#productName').val() }
    });
    

    You may also need to specify a couple other options if they don't default correctly. Explicit code is generally better than implicit in many cases:

    $.ajax({
        url: 'yourUrl.php',
        type: "POST",
        dataType: 'html',
        data: { productName: $('#productName').val() }
    });
    

    In general you'll probably want to check the documentation for $.ajax() and see what you can and should tell it. You'll also want to take a look at your browser's debugging tools to see more specifically why and how it fails when testing these things.

    Speaking of things you should do, you should read this and this. Your code is wide open to SQL injection attacks at the moment, which basically means that you are executing as code anything your users send you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿