将选择的值存储到变量中 [英] Storing the value of a select into a variable

查看:93
本文介绍了将选择的值存储到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一点帮助下,我创建了一个PHP生成的< select> 元素。

With a little help, I have created a PHP generated <select> element.

<select>
    <?php
        for($optionIndex=1; $optionIndex <= 12; $optionIndex++){
        echo '<option>' . $optionIndex . '</option>';
    } ?>
</select>

现在,一旦用户选择了其中一个选项(1-12),我想要存储该值变为php变量 $ number 。我怎么能这样做?

Now, once a user selects one of the options (1-12), I would like to store that value into a php variable $number. How might I go about this?

推荐答案

你必须使用一个表格,用GET或POST发送数据给你的PHP脚本。

You have to use a form which sends data either with GET or POST to your php script.

在页面上:

On the page:

<form method="POST" action="path/to/your/script">

    <select name="select_name">
        <option value="1">1</option>
    </select>

    <input type="submit">
</form>

PHP script

PHP script

<?php
    $number = $_POST['select_name'];

更新:您可以在一页上执行此操作:

UPDATE: You can do this on one page:

<?php

if(empty($_POST['select_name'])) {
    // HERE COMES YOUR FORM
} else {
    $number = $_POST['select_name'];
}

这篇关于将选择的值存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆