简体   繁体   English

选择选项时的回声值-仅PHP

[英]Echo value when select an option - PHP Only

I am just starting to learn php and I have some problem with my little project. 我刚刚开始学习php,我的小项目有些问题。 I create a DB that contain "members" table with "member_id" and "member_name" calumn. 我创建一个数据库,其中包含具有“ member_id”和“ member_name”值的“ members”表。 I create a select box that is populated with members name. 我创建一个选择框,其中填充了成员名称。 When I select a name and I click to submit button, I want to echo the member_id. 当我选择一个名称并单击提交按钮时,我想回显member_id。 How can I do that without javascript/ajax. 没有javascript / ajax怎么办?

PS. PS。 I want to stay on the current page after submiting. 提交后,我想停留在当前页面上。

Thank you very much. 非常感谢你。

<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>

You can get form submit values like this 您可以像这样获取表单提交值

if(isset($_POST['numeselect'])){
    echo $_POST['numeselect'];
}

Change options to this 更改此选项

echo '<option value="'.$nume_selectat['id_membru'].'">'.$nume_selectat['nume_membru'].'</option>';

So the full script 所以完整的剧本

<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>
<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>

What did I add? 我添加了什么?

                if(isset($_POST)) {
                  $numeselect = $_POST["numeselect"];
                  echo $numeselect;
                }

How can I do that without javascript/ajax. 没有javascript / ajax怎么办?

PS. PS。 I want to stay on the current page after submiting. 提交后,我想停留在当前页面上。

If by stay on the current page you mean NO page refresh, and you don't want to use javascript, it's not possible. 如果停留在当前页面上是指没有页面刷新,并且您不想使用javascript,则不可能。

If you just mean you want to POST to the same page, then you would echo the variable in the $_POST array. 如果仅是要发布到同一页面,则可以在$_POST数组中echo该变量。

You could use switch case, and 2 functions, first for the form, second for show value with code of Joni Salmi, example: 您可以使用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'];
  }

}

?>

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

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