简体   繁体   中英

send two values to ajax query

I need to send two parameter to my javascript and get the parameters in the php file how I can do it ?

My html :

<form method="post" action="testButtonSup.php">
        <p>
           Veuillez choisir le service<br />
           <input type="radio" name="service" value="ncli" id="ncli" checked ="checked" /> <label for="ncli">Ncli</label>
           <input type="radio" name="service" value="fcli" id="fcli" /> <label for="fcli">Fcli</label>
        </p>
        <p>
            <label for="client">Veuillez choisir le fournisseur :</label><br />
               <select name="client" id="client" onchange="showUser(this.value, service)">
                    <?php 
                        // echo '<option value=""/></option>';
                        while ($donnees = $reponse->fetch())
                        {               
                            echo '<option value='.$donnees['refCustomer'].'>'.$donnees['legalCompanyName'].' </option>';
                            $idClient = $donnees['refCustomer'];                                
                            //$value = $donnees['refCustomer'];                     
                        }


                        $reponse->closeCursor();
                    ?>              
               </select>
            </p>
        <p>.....

I want to send to the function showUser(this.value, service) two parameters : the id of the select and the value of the radio button "service" whic is up

My function :

function showUser(str, service) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","getTableBuffer.php?q="+str+"&service="service,true);
            xmlhttp.send();
        }
    }

I tried like this but it doesn't work.

in my php file it didn't recognize the parameter.

It works with only the id of the select.

Without jQuery, a smallish function to get the value of a radio button from the collection to use in the ajax parameters.

    function radiovalue(name){
        var col=document.querySelectorAll('input[type="radio"]');
        for( var n in col )if( n && col[n] && col[n].nodeType==1 && col[n].type=='radio' ){
            if( col[n].hasAttribute('name') && col[n].getAttribute('name')==name ){
                if( col[n].checked ) return col[n].value;
            }
        }
        return false;
    }

   eg:
   ---
   xmlhttp.open("GET","getTableBuffer.php?q="+str+"&service="+radiovalue('service'),true);

If you can use jQuery (which I recommend to handle ajax request) you can do it this way:

function showUser(str, service) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else { 
    $.ajax({
      url: 'getTableBuffer.php',
      type: 'GET',
      data: {q:str, service:service}
    }).done(function(response){
      // Code to execute once the call has been executed
      $('#txtHint').html(response.responseText);
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM