Send two parameters at once via ajax

Asked

Viewed 107 times

-1

I am sending an information with ajax, but I would like to send two at once. It would be possible to adapt the code below for this ?

  <select name="sex" class="" required>
<option value="1"> Woman </option>
<option value="2">Man </option>
</select>


     <select name="country" class="country select" required>
<option value="1">Exemple 1</option>
<option value="2">Exemple 2</option>
<option value="3">Exemplo 3</option>
</select>

The javascript is this

$(document).ready(function()
{
$(".country").change(function()

{
var country_id=$(this).val();
var post_id = 'id='+ country_id;

$.ajax
({
type: "POST",
url: "../conexao/ajax.php",
data: post_id, 
cache: false,
success: function(cities)
{
$(".city").html(cities);
} 
});

});
});

In the php file you receive, I have this

  <?php
include('conexcao.php');
if($_POST['id']){
$id=$_POST['id'];
$sex = $_POST['sex'];

if($id==0){
 echo "<option>Selecione seu nome</option>";
}else{
 $sql = mysqli_query($conn,"SELECT * FROM `teste` WHERE sex = $sex");
 while($row = mysqli_fetch_array($sql)){
 echo '<option value="'.$row['id'].'">'.$row['nome'].'</option>';
 }
 }
}
?>

1 answer

1


Pass an additional parameter on the date of your ajax:

data: {id: $('.country').val() , sex: $('.sex').val()},

The two selected select information will be sent in html.

  • Hello, first thanks for trying to help. I made the replacement as you suggested, on the part of my ajax. REPLACES >> date: post_id, << by the code you said, but it didn’t work... :(

  • 1

    Add the class="sex" in the SEX input , delete these two lines " var country_id=$(this). val(); "and "var post_id = 'id='+ country_id;" . Now make sure it doesn’t work. and put the code I sent you instead of "DATA" in ajax.

  • Perfect, it worked Bartollo, you’re beast, hugs !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.