0

I am using PHP, mysql and ajax. I have one php file called Billing.php where i have 2 dropdowns one named Company and the other named Model Name.

I am able to display all the company names and model names in both dropdown by using sql query.

But now it fetches all the data available in both the dropdown. My concern is that i want to select one company name and second dropdown should filter the model name on the basis of company.

After looking for some solution online i came to know we can use ajax for that but i don't know what i am missing in my code. It is giving me error as well

Notice: Undefined index: ajaxcompanyname in C:\Users\Shesharm\MyWebsite\Billing.php

Where ajaxcompanyname is the data passed from ajax function to php file which i am using to fetch query result for Model name dropdown.

Billing.PHP

<!DOCTYPE html>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style>


table,th,td{
    border: 1px solid black;
    border-collapse: collapse;
    text-align: left;
    table-layout: auto;
    background-color: #CCCCCC;
}
</style>

 <script type="text/javascript" src="Billing_Selection.js"></script>
</head>

<body>


<?php
//Define variables 
$company=$ModelName=$ModelNo=$productcolor=$cost=$sql1=$sql2=$IMEI=$accessories=$row=$quantity=$customername=$customerinfo=$prodstatus="";
$quantityerr=$q="";

?>


<form name="SearchMobile" method="POST" action="<?php $_SERVER['PHP_SELF'];?>" style="background-color:#FF6600;">
    <fieldset>
        <legend><b>Search the Required mobile</b>   </legend>
    <?php   
    // Connect to database and get the no of rows in $num
        $con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
        $sql1 = "select DISTINCT CompanyName from mobile_data";

        //check connection
        if(!$con){
            echo "Failed to connect to MySQL: " . mysql_error();
        }


        $retval1 = mysqli_query($con,$sql1);

        echo "<select name='companyname' onchange='companysortlist(this.value)'>";
        echo "<option value=''>Select a Mobile:</option>";
        while($row = mysqli_fetch_array($retval1)){
            echo "<option value='".$row['CompanyName']."'>".$row['CompanyName']."</option>";
        }
        echo "</select>"."&nbsp; ";


        //******************* Select the model name..
        $sql2 = "select DISTINCT  ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
        //check connection
        if(!$con){
            echo "Failed to connect to MySQL: " . mysql_error();
        }

        echo "<select name='modelname' class='companymodelname'>";
        echo "<option value=''>Select a Model Name:</option>";

        $retval1 = mysqli_query($con,$sql2);
        while($row = mysqli_fetch_array($retval1)){
            echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
            }
        echo "</select>"."&nbsp; ";



        mysqli_close($con);
        ?>
    </fieldset>
</form>
<br>




</body>

</html>

Billing_Selection.js

function companysortlist(data){

$.ajax({

    type:'POST',
    url:'Billing.php',
    data:"ajaxcompanyname="+data,
    success:function(data){
        $('.companymodelname').html(data);
    }
}

)

}

Please let me know what i am doing wrong.I am beginner in all these.

Hmmm
  • 1,774
  • 2
  • 13
  • 23
Shesharm
  • 35
  • 1
  • 10

2 Answers2

0

Ajax allows you to make additional requests to the server after the page has loaded hence the name ajax:

Asynchronous Javascript And Xml

You're getting Undefined index: ajaxcompanyname

because the POST variable ajaxcompanyname is not set on the initial page load, that comes with a later ajax request.

You can make ajax requests to the same url that the page loads from initially but it requires additional logic to determine if the request is an ajax request or not. I would advise against it and create a separate file for you ajax request eg:

ajaxcompanyname.php

which would look like:

    $con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
    //******************* Select the model name..
    $sql2 = "select DISTINCT  ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
    //check connection
    if(!$con){
        echo "Failed to connect to MySQL: " . mysql_error();
    }

    echo "<select name='modelname' class='companymodelname'>";
    echo "<option value=''>Select a Model Name:</option>";

    $retval1 = mysqli_query($con,$sql2);
    while($row = mysqli_fetch_array($retval1)){
        echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
        }
    echo "</select>"."&nbsp; ";
    mysqli_close($con);

You would then change

 url:'Billing.php',

To

  url:'ajaxcompanyname.php',

and remove the duplicate code from Billing.php

Edit

I thought I should mention that I have not attempted to fix any of the sql injection vulnerabilities in your code. This is something you should look into

Community
  • 1
  • 1
andrew
  • 9,313
  • 7
  • 30
  • 61
  • Thanks Andrew for the explanation..but i think i was not able to address the problem in a clear way. SO i Would mention step by step what i have done. Please look into that and let me know what exactly need to change. (1). Create file 'Billing.php' and put the dropdown code there.In first dropdown , I am calling the ajax function 'onchange='companysortlist(this.value)'. (2). Ajax function i kept in a separate js file named as 'Billing_Selection.js'. Function you can see above. (3). In Function, URL is given as 'Billing.php' only where i have dropdowns code. – Shesharm Dec 12 '14 at 05:15
  • (4) In ajax function,'companymodelname' is a class of second dropdown. (5) Now, in 'Billing.php' file i am checking 'ajaxcompanyname' which is passed from the ajax function. It is used in a query to fetch the result e.g. ajaxcompanyname= 'Nokia' and it will be used to fetch the ModelNAme through a query. Which i can display in my second dropdown. Note: I have doubt about my ajax function if i am doing it correctly or not. And how to initialize the variable 'ajaxcompanyname' in BIlling.php file so it does not give error 'undefined index'. – Shesharm Dec 12 '14 at 05:23
  • I thought I'd explained it clearly, what do you think will happen when you try to populate the second select box from billing.php? Think about it. you're going to load the entire page into the select box which obviously wont work. move the relevant part of the code to a separate php file as explained above – andrew Dec 12 '14 at 12:34
  • I tried the same as you said above. But now second select box disappear at all. Even i tried with include command as well to add the file in 'Billing.php'. I am struggling with ajax more. What i want is that i want to know the value selected in first select box so i can run sql query to fetch items in second select box with first select box value. That's why i was passing the value to ajax function so i can pass the same value back to php file from where the function was called. So i can use that value as a condition in my query.Could you please help on this?How to return value backto phpfile? – Shesharm Dec 13 '14 at 08:23
  • Awesome Andrew.. It is working.. You saved my lot of time which i was spending here and there for solution.. Thank you very much.. And i will surely take care of sql injection.. :) – Shesharm Dec 16 '14 at 17:59
0

Simplified Answer:

I've omitted some database checking etc to keep it short.

Billing.php

 <?php
 $con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
 $sql = "select DISTINCT CompanyName from mobile_data";
 $retval = mysqli_query($con,$sql);
 ?>

<form method="POST">
   <select name="companyname" onchange="companysortlist(this.value)">
      <option value=''>Select a Mobile:</option>
      <?php 
        while($row = mysqli_fetch_array($retval)){
         echo "<option value='".$row['CompanyName']."'>".$row['CompanyName']."</option>";
         }
      ?>
   </select>

   <select name="modelname" class="companymodelname">
       <!-- note the select is completely empty, it will be fill by ajax -->
   </select>
</form>

<script>
function companysortlist(data){
  $.ajax({
      type: 'POST',
      url: 'ajax.php',
      data: "ajaxcompanyname=" + data,
     success: function (data) {
          $('.companymodelname').html(data);
      }
  });
}
</script>

ajax.php

<?php
 $con = mysqli_connect("MyWebsite.localdomain","root","shekhar123","test_youtube");
 $sql = "select DISTINCT  ModelName from mobile_data where CompanyName='".$_POST['ajaxcompanyname']."'";
 $retval = mysqli_query($con,$sql);


 while($row = mysqli_fetch_array($retval)){
      echo "<option value='".$row['ModelName']."'>".$row['ModelName']."</option>";
  }
 ?>

Please note

I still have not fixed any of the sql injection vulnerabilities in your code

andrew
  • 9,313
  • 7
  • 30
  • 61