[英]How to update a field on the form based on a select option
I have a PHP form with the following select list; 我有一个带有以下选择列表的PHP表单;
<select id ="MatchCaptain" name="MatchCaptain" onchange="findTeleNo(this.value)"
<?php
$MC = $_SESSION["MatchCapt"];
player_load($MC);
?>
>
</select>
I also have a text field ; 我也有一个文本字段;
Telephone Number: </b> <?php echo $_SESSION["TeleNo"]; ?></p>
The PHP function called by the onchange command is ; onchange命令调用的PHP函数是;
function findTeleNo($MatchCaptain){
$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$database = "matchmanagementDB";
$db_found = mysqli_select_db($db_handle, $database);
if ($db_found) {
$SQL = "SELECT * FROM `playerstb` ORDER BY `Surname` ASC, `FirstName` ASC";
$result = mysqli_query($db_handle, $SQL);
$ufullName = split_name($MatchCaptain);
while ( $db_field = mysqli_fetch_assoc($result) ) {
$uName = $db_field['FirstName'];
$uName = trim($uName);
$Surname = $db_field['Surname'];
$Surname = trim($Surname);
$fullName = $uName." ".$Surname;
if ($fullName == $ufullName )
{
$_SESSION["TeleNo"] = $db_field['TeleNo'];
include "Match_sort.php";
break;
}
}
}
}
What I am trying to do is when the Match Captains name is changed in the SELECT dropdown list then I want the FUNCTION findTeleNo() to run. 我想做的是,当SELECT下拉列表中的Match Captains名称更改时,我希望FUNCTION findTeleNo()运行。 Which should then reload the form with the telephone number of the New Match Captain. 然后,该表格应重新填写新比赛队长的电话号码。 However, when I select a new Match Captain the onchange command is ignored. 但是,当我选择一个新的比赛队长时,onchange命令将被忽略。 As a Septuagenarian, just learning this language, I need some help! 作为七十士译本,只要学习这种语言,我就需要一些帮助! Does onchange work in PHP? onchange是否可以在PHP中使用? If not what should I use? 如果没有,我应该怎么用?
The onchange event is a javascript event, it cannot call your php function directly. onchange事件是一个javascript事件,它无法直接调用您的php函数。 You can create a javascript function that will be called when the selects value changes and then this can make an xhr(Ajax) request to a php file which will perform a database query and return what you need it to. 您可以创建一个JavaScript函数,当选择值更改时将被调用,然后可以向php文件发出xhr(Ajax)请求,该文件将执行数据库查询并返回您需要的内容。 You could then update the page with javascript. 然后,您可以使用javascript更新页面。
PHP is a server side language, you need to use javascript for this. PHP是一种服务器端语言,您需要为此使用javascript。
this is example: 这是示例:
function findTeleNo (value) { console.log(value); $.ajax({ url: 'findTeleNo.php', data: { c_name: value }, success: function (response) { $('#cname').text(response.cname); $('#teleno').text(response.teleno); } }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <select id ="MatchCaptain" name="MatchCaptain" onchange="findTeleNo(this.value)"> <option>choose one</option> <option value="A1">A1</option> <option value="B2">B2</option> <option value="C3">C3</option> </select> <div> <div> Captain Name is: <span id="cname">empty</span> <div> <div> TeleNo is: <span id="teleno">empty</span> <div>
and in file findTeleNo.php you print json has cname and teleno. 在文件findTeleNo.php中,您打印的json具有cname和teleno。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.