简体   繁体   English

使用select1上的选定值填充select2,而不刷新页面或使用按钮

[英]fill select2 using selected value on select1 without refresh page or using a button

I'm a Beginner on PHP, Javascript, I would like to fill select "info" using selected value on select "peoplesnames" without refresh page or using a button 我是PHP,Javascript的初学者,我想使用选择“ peoplesnames”上的选定值来填充选择“ info”,而无需刷新页面或使用按钮

this is my code 这是我的代码

HTML: HTML:

<select id="peoplesnames" name="peoplesnames" onchange=" $person<<--Somthings here to get selected ">

   ` <option value="1">john</option>`
    `<option value="2">sarah</option>`
   ` <option value="3">samantha</option>`
`</select>`

PHP 的PHP

<?php

echo '<select style="width: 110px; name="infos">';

$requete = mysql_query('SELECT city FROM people where id=$person');

$requete2 = mysql_query('SELECT age FROM people where id=$person');

$nmbrrows = mysql_numrows($requete);

for ($i=0; $i<$nmbrrows ; $i++) {

$city = mysql_result($requete,$i);

$age = mysql_result($requete2,$i);

echo '<option value="'.$age.'"> '.$city.' </option>';

}

echo '</select>';

?>

what need I add to my code? 我需要添加什么代码? please tell me if there is another solution, Thanks 请告诉我是否还有其他解决方案,谢谢

I would suggest doing some jQuery to detect a change event on the select box, and then detecting what the selected value is. 我建议做一些jQuery来检测选择框上的更改事件,然后检测选择的值是什么。

On the backend i would create a PHP end point that received the selected id/value, and returned the SQL results you want. 在后端,我将创建一个PHP端点,该端点接收选定的ID /值,并返回所需的SQL结果。

Using the on change event, pass the selected value to your php code using Jquery $.post and then take the response and update a division on your page with that users info. 使用on change事件,使用Jquery $ .post将选定的值传递到您的php代码,然后获取响应并使用该用户信息更新页面上的分区。

An Example would be: 一个例子是:

$("#peoplesnames").change(function(){
    var id = $('#peoplesnames :selected').val();
    $.post( "test.php", { id: id}).done(function( data ) {
          $("#info p").text(data);
       });

});

This will detect when the select value has changed, grab the value of the selected item, pass that value to your PHP script, take the response from it and add it to a <p> tag in the #info division. 这将检测选择值何时更改,获取所选项目的值,将该值传递给您的PHP脚本,从中获取响应并将其添加到#info分区中的<p>标记中。

You PHP Script could look something like: 您的PHP脚本可能类似于:

<?php
   $id = $_POST['id'];
   $request = mysql_query('SELECT * FROM people where id = $id');
   $data = mysql_result($request);

   $response = "The person named ".$data['name']." is ".$data['age']." and lives in ".$data['city']." Happily everafter.";
   return $response;

 ?>

You of course will need to format that however you want, but that should pull in the specific person's data and populate a division each time you change the combobox's selection. 当然,您将需要设置所需的格式,但是每次更改组合框的选择时,都应提取特定人员的数据并填充一个分区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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