我刚开始学习php,我的小项目有些问题。 我创建一个DB,其中包含“members”表,其中包含“member_id”和“member_name”calumn。 我创建了一个填充了成员名称的选择框。当我选择一个名称并单击提交按钮时,我想回显member_id。如果没有javascript / ajax,我怎么能这样做。
PS。我想在提交后留在当前页面。
非常感谢。
<html>
<body>
<h2>Select</h2>
<table>
<tbody>
<tr>
<td>
<form method="post" action="<?=($_SERVER['PHP_SELF'])?>">
<table>
<thead>
<tr>
<th>Nume</th>
<th>Id</th>
</tr>
</thead>
<tr>
<td><select name="numeselect">
<?php
$numeselect=$_POST ["numeselect"];
$host="localhost";
$user="***";
$password="***";
$db_name="***";
$con=mysqli_connect($host,$user,$password,$db_name);
if(mysqli_connect_errno($con)){
echo "Eroare la coenxiune:" . mysqli_error();
}
$selectare_nume = mysqli_query($con,"SELECT nume_membru, id_membru FROM membri");
while($nume_selectat = mysqli_fetch_array($selectare_nume)){
echo '<option value="'.$nume_selectat['nume_membru'].'">'.$nume_selectat['nume_membru'].'</option>';
}
echo '</select></td>';
while($id_selectat = mysqli_fetch_array($selectare_nume)){
echo '<td>'.$id_selectat['$id_membru'].'</td>';
}
echo '</tr></table>';
?>
<input type="submit" value="show id">
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
答案 0 :(得分:1)
您可以获得此类表单提交值
if(isset($_POST['numeselect'])){
echo $_POST['numeselect'];
}
将选项更改为此
echo '<option value="'.$nume_selectat['id_membru'].'">'.$nume_selectat['nume_membru'].'</option>';
所以完整的脚本
<html>
<body>
<h2>Select</h2>
<table>
<tbody>
<tr>
<td>
<form method="post" action="<?=($_SERVER['PHP_SELF'])?>">
<table>
<thead>
<tr>
<th>Nume</th>
<th>Id</th>
</tr>
</thead>
<tr>
<td><select name="numeselect">
<?php
$numeselect=$_POST ["numeselect"];
$host="localhost";
$user="***";
$password="***";
$db_name="***";
$con=mysqli_connect($host,$user,$password,$db_name);
if(mysqli_connect_errno($con)){
echo "Eroare la coenxiune:" . mysqli_error();
}
$selectare_nume = mysqli_query($con,"SELECT nume_membru, id_membru FROM membri");
while($nume_selectat = mysqli_fetch_array($selectare_nume)){
echo '<option value="'.$nume_selectat['id_membru'].'">'.$nume_selectat['nume_membru'].'</option>';
}
echo '</select></td>';
while($id_selectat = mysqli_fetch_array($selectare_nume)){
echo '<td>'.$id_selectat['$id_membru'].'</td>';
}
echo '</tr></table>';
?>
<input type="submit" value="show id">
</form>
</td>
</tr>
</tbody>
</table>
<?php
if(isset($_POST['numeselect'])){
echo "<p>You selected member id: {$_POST['numeselect']}</p>";
}
?>
</body>
</html>
答案 1 :(得分:0)
<html>
<body>
<h2>Select</h2>
<table>
<tbody>
<tr>
<td>
<form method="post" action="<?=($_SERVER['PHP_SELF'])?>">
<table>
<thead>
<tr>
<th>Nume</th>
<th>Id</th>
</tr>
</thead>
<tr>
<td>
<?php
if(isset($_POST)) {
$numeselect = $_POST["numeselect"];
echo $numeselect;
}
?>
<select name="numeselect">
<?php
$host="localhost";
$user="***";
$password="***";
$db_name="***";
$con=mysqli_connect($host,$user,$password,$db_name);
if(mysqli_connect_errno($con)){
echo "Eroare la coenxiune:" . mysqli_error();
}
$selectare_nume = mysqli_query($con,"SELECT nume_membru, id_membru FROM membri");
while($nume_selectat = mysqli_fetch_array($selectare_nume)){
echo '<option value="'.$nume_selectat['nume_membru'].'">'.$nume_selectat['nume_membru'].'</option>';
}
echo '</select></td>';
while($id_selectat = mysqli_fetch_array($selectare_nume)){
echo '<td>'.$id_selectat['$id_membru'].'</td>';
}
echo '</tr></table>';
?>
<input type="submit" value="show id">
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
我添加了什么?
if(isset($_POST)) {
$numeselect = $_POST["numeselect"];
echo $numeselect;
}
答案 2 :(得分:0)
如果没有javascript / ajax,我怎么能这样做。
PS。我想在提交后留在当前页面。
如果留在当前页面上,则表示没有页面刷新,并且您不想使用javascript,这是不可能的。
如果您只是想要POST到同一页面,那么您将echo
$_POST
数组中的变量。
答案 3 :(得分:0)
你可以使用switch case和2个函数,第一个用于表单,第二个用于显示值,代码为Joni Salmi,例如:
<?php
// action
$action = $_POST['action'];
if($action == "undefined" || $action == "") $action = "form_member";
switch($action){
// Member form
case 'form_member':
form_member();
break;
// Show Member id
case 'show_member':
show_member();
break;
}
function form_member(){
?>
<html>
<body>
<h2>Select</h2>
<table>
<tbody>
<tr>
<td>
<form method="post" action="<?=($_SERVER['PHP_SELF'])?>">
<input type="hidden" name="action" value="show_member" />
<table>
<thead>
<tr>
<th>Nume</th>
<th>Id</th>
</tr>
</thead>
<tr>
<td><select name="numeselect">
<?php
$numeselect=$_POST ["numeselect"];
$host="localhost";
$user="***";
$password="***";
$db_name="***";
$con=mysqli_connect($host,$user,$password,$db_name);
if(mysqli_connect_errno($con)){
echo "Eroare la coenxiune:" . mysqli_error();
}
$selectare_nume = mysqli_query($con,"SELECT nume_membru, id_membru FROM membri");
while($nume_selectat = mysqli_fetch_array($selectare_nume)){
echo '<option value="'.$nume_selectat['nume_membru'].'">'.$nume_selectat['nume_membru'].'</option>';
}
echo '</select></td>';
while($id_selectat = mysqli_fetch_array($selectare_nume)){
echo '<td>'.$id_selectat['$id_membru'].'</td>';
}
echo '</tr></table>';
?>
<input type="submit" value="show id">
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
<?php
}
function show_member(){
if(isset($_POST['numeselect'])){
echo $_POST['numeselect'];
}
}
?>