简体   繁体   English

PHP:如何基于select元素的值更新html文本元素?

[英]PHP: how to update html text element, based on value from select element?

I have an an html form with only <select> element. 我只有一个<select>元素的html表单。 Below the form I have a text box, I want to update the value of the text box based on the option selected in the form. 在表单下面,我有一个文本框,我想根据表单中选择的选项来更新文本框的值。

I am not sure if this is possible in PHP, I know I can do it with Javascript/Jquery, but I am working on a school project and its a PHP project. 我不确定在PHP中是否可行,我知道我可以使用Javascript / Jquery做到这一点,但是我正在研究一个学校项目及其PHP项目。 My teacher has done it but Im not sure if he used PHP. 我的老师已经做到了,但是我不确定他是否使用过PHP。

Here is my code: 这是我的代码:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="books">
        <?php
            $books = simplexml_load_file("books.xml");
            foreach($books->book as $book){
                $code = $book->code;
                $title = $book->title;
                echo "<option value='$code'>$code: $title</option>";
            }
        ?>
    </select>
</form>

<?php
    $choice = $_POST['books'];
    echo "<input type='text' value='$choice'>";
?>

When the page first loads I want the value from the selected option to be in the text box. 当页面第一次加载时,我希望所选选项的值在文本框中。 I am not sure how I could go about doing this in PHP. 我不确定如何在PHP中执行此操作。 Help would be appreciated. 帮助将不胜感激。

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="books" onchange="javascript:document.form.submit();">
        <?php
            $books = simplexml_load_file("books.xml");
            foreach($books->book as $book){
                $code = $book->code;
                $title = $book->title;
                echo "<option value='$code'>$code: $title</option>";
            }
        ?>
    </select>
</form>

<?php
    $choice = isset($_POST['books'])?$_POST['books']:'';
    echo "<input type='text' value='$choice'>";
?>

When you first load the page, there are no POST data, so you could set $choice to the first book in list: 首次加载页面时,没有POST数据,因此可以将$choice设置$choice列表中的第一本书:

$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;

But you have to udnerstand the difference between PHP and JavaScript in here. 但是您必须在这里了解PHP和JavaScript之间的区别。

PHP can only change the value, when there is a request on the server as it is a server-side language. 当服务器上有请求时,PHP只能更改该值,因为它是服务器端语言。 If you wish to update the value instantly, without submiting the form, you need to use JavaScript. 如果您希望立即更新值而不提交表单,则需要使用JavaScript。

if it must be purely PHP, then put a submit button on your form for when a selection is made. 如果必须是纯PHP,则在表单上放置一个提交按钮以进行选择。 This will set the value you are looking for. 这将设置您要查找的值。 Others have provided samples on how to do this form submission automatically with javascript, but the end result is the same. 其他人提供了有关如何使用javascript自动完成此表单提交的示例,但最终结果是相同的。

Edit , as a pure PHP solution with a submit button, which will load data on initial page load then load data with the submit button as per selection. Edit ,作为带有提交按钮的纯PHP解决方案,它将在初始页面加载时加载数据,然后根据选择使用提交按钮加载数据。

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="books">
        <?php
            $books = simplexml_load_file("books.xml");
            foreach($books->book as $book){
                $code = $book->code;
                $title = $book->title;
                echo "<option value='$code'>$code: $title</option>";
            }
        ?>
    </select>


<input type="submit" value="submit" name="submit">

</form>

<?php 

if(isset($_POST['submit'])){
$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;
    echo "<input type='text' value='$choice'>";
}

else{
$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;
    echo "<input type='text' value='$choice'>";
}

?>

Original answer with onchange onchange原始答案

The following would work, using Javascript and getElementById which would echo the selection in a text box using an onchange function. 使用Javascript和getElementById ,以下代码将起作用,它们将使用onchange函数在文本框中进行选择。

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="books" onchange="update_textbox()" id="id_books">
        <?php
            $books = simplexml_load_file("books.xml");
            foreach($books->book as $book){
                $code = $book->code;
                $title = $book->title;
                echo "<option value='$code'>$code: $title</option>";
            }
        ?>
    </select>

</form>

<?php 

    $choice = isset($_POST['books']) ? $_POST['books'] : '';
    echo "<input type='text' value='$choice' id='showit'>";

?>

<script>
 function update_textbox(){
  var selectbox = document.getElementById('id_books');
  var id_books = selectbox[selectbox.selectedIndex].text;
  document.getElementById('showit').value=id_books ;
 }
</script>

To load data into the text box at initial page load, use: 要将数据在初始页面加载时加载到文本框中,请使用:

Replace: 更换:

$choice = isset($_POST['books']) ? $_POST['books'] : '';

with: 有:

$choice = isset($_POST['books']) ? $_POST['books'] : $books->book[0]->code;

You can do it with PHP, it looks as though you're just missing the submit button to actually post the selection. 您可以使用PHP进行操作,看起来好像只是缺少实际提交选择的“提交”按钮。

To submit the form without a submit button, you need to use javascript, eg. 要提交没有提交按钮的表单,您需要使用javascript,例如。

<select onchange="this.form.submit()">....</select>

Below is a simple example (static options rather than xml) that also sets the selected option in the select (IMO this is a more practical use than printing it in the text input) 下面是一个简单的示例(静态选项而不是xml),该示例还在select中设置了选定的选项(IMO比在文本输入中打印它更实用)

<?php
$val = isset($_REQUEST['books']) ? $_REQUEST['books'] : null;
?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <select name="books" onchange="this.form.submit()">
        <option value=>Choose a book</option>
        <option<?php if($val && $val === 'opt 1'){ print ' selected';}?>>opt 1</option>
        <option<?php if($val && $val === 'opt 2'){ print ' selected';}?>>opt 2</option>
    </select>

    <noscript><button type="submit">Submit</button></noscript>
</form>

<?php if($val){ ?>
<input type="text" value="<?php echo $_REQUEST['books']; ?>">
<?php } ?>

This is probably ok just for a school exercise, but in the real world you would probably want to sanitize any user input ($_REQUEST['book'] / $_POST['book']) and use a better templating engine than vanilla PHP (I like Twig) to help separate presentation from functional logic. 这可能只适合学校练习,但是在现实世界中,您可能希望清理任何用户输入($ _REQUEST ['book'] / $ _POST ['book']),并使用比原始PHP更好的模板引擎。 (我喜欢Twig)有助于将表示与功能逻辑分开。

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

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