Skip to content Skip to sidebar Skip to footer

Passing Two Variable From Php To Ajax Then Back To Php

I'm new to ajax so I don't know much about ajax syntax. though i am trying here to pass variable from php to ajax and then back to php. I was able able to get it done with one vari

Solution 1:

If I get that correctly, your initial problem is to pass more than one variable in AJAX call.

You can construct the URL like this :

var val2 = $("#program_number").val();
var val1 = $("#program").val();
$.get("addnewbug2.php?program_number="+val2+"&program_name="+val1)

Solution 2:

json_encode() is perfect for transport php variable with ajax

something like

//your ajax call receiver page -: your_link_for_ajax.php

$output =     json_encode(array('type'=>'success','address'=>$address,'table_record'=>$table_record));
    die($output);

you can read it like

this code on your html page

    $.post('your_link_for_ajax', post_data, function(response){  

                    //load json data from server and output message     if(response.type == 'error')
                    {
                        output = '<div class="alert alert-danger">'+response.text+'</div>';
                    }else{
                        output = '<div class=" alert alert-success">'+response.address+'</div>';
                        $('#stateIdContact').html(response.table_record);

                    }

                }, 'json');

Solution 3:

Thank you for replying, i solved my problem using AngularJS, which i started reading after posting this question. My main moto behind this question was to populate a (2nd)select box and (3rd)select box using value from other (1st) select box and when the values in newly populate (2nd) select box was done. Then change values (3rd)select box using the (2nd) select box. Vice versa for last point. Apply unique filter to select boxes and without moving to next page. So here's the code simplified form of Addnewbug.php-

<!DOCTYPE html><html ><scriptsrc= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script><scriptsrc= "https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js"></script><body><divng-app="myApp"ng-controller="customersCtrl"><selectng-model="selectProgram"><option></option><optionng-repeat="x in programes | unique: 'prognumber'"name=""{{x.progname}}"">{{ x.progname }}</option></select><selectng-model="selectNumber"><option></option><optionng-repeat="x in programes | filterBy: ['progname']:selectProgram | filterBy: ['progrelease']:selectRelease | unique: 'prognumber'"name=""{{x.prognumber}}"">{{ x.prognumber }}</option></select><selectng-model="selectRelease"><option></option><optionng-repeat="x in programes | filterBy: ['progname']:selectProgram | filterBy: ['prognumber']:selectNumber | unique: 'progrelease'"name=""{{x.progrelease}}"">{{ x.progrelease }}</option></select></div><script>var app = angular.module('myApp', ['angular.filter']);
          app.controller('customersCtrl', function($scope, $http) {
             $http.get("db/program_db.php")
             .success(function (response) {$scope.programes = response.records;});
          });
          </script></body></html>

offcourse i used some other stackexchange questions to get it right. And for the table which is being imported in this code i used this program_db-

<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    if(!isset($conn)){
            //Database Connectivity - ip, username, password, database name$conn = new mysqli("*connection parameters*");
     }
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    }
    $result = mysqli_query($conn, "SELECT program_name, program_number,program_release FROM program");
    $outp = "";
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
        if ($outp != "") {$outp .= ",";}
        $outp .= '{"progname":"'  . $rs["program_name"] . '",';
        $outp .= '"prognumber":"'   . $rs["program_number"]        . '",';
        $outp .= '"progrelease":"'. $rs["program_release"]     . '"}';
    }
    $outp ='{"records":['.$outp.']}';
    $conn->close();

    echo($outp);
    ?>

Solution 4:

I have same problem as you had in this page: PHP ajax database : how to pass two variables and get data of them in different options? ,i have 2 diffrent php file,one is handling select files,the other handle the database sql,firstly i pass a value which user choosed from first select which updates the second select,and choosing second select should update third select(which is remain empty) i dont know what is my problem(i guess i send value of first ajax to the other php file,and when i get it back to use it for second ajax call,the first value will gone.

Post a Comment for "Passing Two Variable From Php To Ajax Then Back To Php"