0

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.

AHJeebon
  • 1,218
  • 1
  • 12
  • 17
APPY SHAH
  • 7
  • 1
  • 6

3 Answers3

1

A PHP library for Ajax

Jaxon is an open source PHP library for easily creating Ajax web applications. It allows into a web page to make direct Ajax calls to PHP classes that will in turn update its content, without reloading the entire page.

Jaxon implements a complete set of PHP functions to define the contents and properties of the web page. Several plugins exist to extend its functionalities and provide integration with various PHP frameworks and CMS.

How does Jaxon work

Define and register your PHP classes with Jaxon.

$jaxon->register(Jaxon::CALLABLE_OBJECT, new MyClass);

Call your classes using the javascript code generated by Jaxon.

<input type="button" onclick="JaxonMyClass.myMethod()" />

check link https://www.jaxon-php.org/docs.html

Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
0
var data1 = "Something";
$.ajax({
    url: "script.php",
    type: "POST",
    data: { data1: data1 }
}).done(function(resp) {
    console.log( resp )
}).fail(function(jqXHR, textStatus) {
    alert("Request failed: " + textStatus + " - Please try again.")
})

Here you have a script that will send the data1 variable across to the php script. The resp in the done portion is the return you send back from the php script.

If you want to send more data just add it { data1: data1, data2: data2 } and so on.

Just adjust to suit your needs.

Blinkydamo
  • 1,582
  • 9
  • 20
0

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.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • i have done the changes but then too the code is not working.I think is_ajax() function is causing problem. – APPY SHAH Oct 26 '16 at 10:50
  • @APPYSHAH: What makes you think that? What debugging have you done to confirm that? I suspect you don't really *need* the `is_ajax()` function anyway. Simply put your AJAX code into its own page and assume an AJAX request. Each server-side resource should handle its own responsibilities, rather than combining multiple different resources into one page with a bunch of `if` statements. – David Oct 26 '16 at 10:51
  • I tried removing is_ajax() but then too it is not working. – APPY SHAH Oct 26 '16 at 11:04
  • @APPYSHAH: "It's not working" doesn't really say anything about the problem. You're going to have to do at least *some* debugging, nobody here can do that for you. – David Oct 26 '16 at 11:11
  • ok David i will do some more debugging and will share the result.Thank you for the help – APPY SHAH Oct 26 '16 at 11:12