Calling PHP using AJAX

Hi Guys

I have a 2 x php files.( stock.php & data.php )
I have figured out somewhat how to call data from my data.php file using AJAX as i dont want to refresh the screen. so basically if you click on the dropdown of items the qty of the item shows.

Question … i want to add another drop down to my stock.php page. using the same principal as above i want to create for example a dropdown of car models and the have my AJAX show the qty. So is there any way i can use my original data.php file and just add another SQL query with php. when i try to do that the first AJAX call for the first dropdown also runs or must i create a seperate PHP file for every dropdown i would like to have ?

Stock.php

<select name="item" id = "item">
    <option></option>
    
    <?php
 
    $sql = "SELECT * from storeitems";
    $result = mysqli_query($link12,$sql);
    while($row= mysqli_fetch_array($result))
    {
        ?>
        
        <option>
        <?php 
        echo $row['name'];
        
    }
?>
</option>
</select>

<div name= "items" id="items">
    
</div>


<script>
            $(document).ready(function(){
                $('#item').change(function(){
                    var items_id = $(this).val();
                    $.ajax({
                        url:"data.php",
                        method:"POST",
                        data:{itemsid:items_id},
                        dataType:"text",
                        success:function(data)
                        {
                            $('#items').html(data);
                        }
                    });
                });
        });

</script>

Data.php

<?php
	include ('dbconnection.php');
$value = $_POST['itemsid'];

$sql = "SELECT * from storeitems where name ='$value'";
$result =mysqli_query($link12,$sql);
while($row=mysqli_fetch_array($result))
{
        ?>
        
      
           <?php 
        echo $row['qty'];
        
    }
?>

Send a second parameter in your data to indicate which query you want to run, and have your PHP code look at it to decide.

While you’re in there, you could / should modify the query you have now to use a prepared statement rather than concatenating a string into it, especially a user-supplied string.

Thanks I think i got it. Can you possibly point me in the right direction.

i have 3 queries working but how do i add up the total of the information retrieved from ajax. IE… the amount stored in #items my second query is #items2

i want to add items and items 2 to get a sum. PHP i can do but need to get the #items , #items2 into a PHP variable

<script>
            $(document).ready(function(){
                $('#item').change(function(){
                    var items_id = $(this).val();
                    $.ajax({
                        url:"data.php",
                        method:"POST",
                        data:{itemsid:items_id},
                        dataType:"text",
                        success:function(data)
                        {
                            $('#items').html(data);
                        }
                    });
                });
        });

I’m a bit confused as to what you’re asking here. Are you asking how to pass the information from #items and #items2, which are page element ids, into your PHP code, or are you asking how to retrieve more than one value from your PHP code, and then paste those values into the above-named page elements?

As I understand it, if you want to return more than one value back from PHP to Ajax, you json-encode the results of your PHP code and set the datatype to be json instead of text.

Sorry for the Confusion.

basically i just want to use the value my ajax returns and assign that value to a php variable which i can then use in my Mysqli

The term “returns” is what is confusing me. To my way of thinking (which might be wrong, I don’t do much JS),

  1. your Ajax code gets a value from your page, in var items_id = $(this).val();
  2. it sends it to PHP, in data:{itemsid: items_id},
  3. your PHP code returns one or more values to Ajax, in echo $row['qty'];
  4. and the Ajax success function inserts that returned information back into the page in $('items').html(data);

Which of those do you want to change / expand?

That value is being returned as example qty is (5) and that value is displayed correctly. So i have more than one query all of them returning qty values ie… (100) , (300) etc. on the end i just want to show on the same page the total IE (405) so basically i need those returned values stored in so i can do a SUM on them. its basicaly a stock bookout —
Item qty = 1
item qty = 10
item qty = 20

TOTAL = 31

Right. You can do two different things here - if all you want is the total, total it up in PHP and return the single figure to Ajax, and then do as you wish with it.

If you need to return each individual number as well as totalling them, that is you need to use the individual figures on the page as well as the total, then you need to json-encode the PHP output so that you can access each field.

In your PHP you could do something like:

$return_array = array("item1"=> $phpvar1, "item2"=>$phpvar2, "item3"=>$phpvar3);
echo json_encode($return_array);

where $phpvar1 and so on are the variables you have extracted from your query results. Back in your Ajax success function:

$('#item1').html(data.item1); 
$('#item2').html(data.item2);
… and so on

where you have appropriate <div id=> for item1 and item2, and you extract the item1, item2 etc. element from the return.

Obviously if you want to return the total as well, just stick it in the array before you encode it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.