获得选择下拉列表的价值

时间:2016-11-14 11:08:42

标签: javascript php jquery html

尝试获取选择下拉列表的值

<select id="utype" class="text" style="width:100%;padding:10px !important" name="user-type">
     <option value="default">Select </option>
     <option value="passenger">Passenger</option>
     <option value="driver">Driver</option>
</select>

但是,即使用户选择乘客

,也始终将驱动程序值插入到db中
if (isset($_POST['submit'])) {

     $date = date('Y-m-d');
     $full_name = mysql_real_escape_string(addslashes($_REQUEST['fname']));
     $email_address = mysql_real_escape_string(addslashes($_REQUEST['email']));
     $pass = mysql_real_escape_string(addslashes($_REQUEST['password']));
     $contact_no = mysql_real_escape_string(addslashes($_REQUEST['mobile']));
     $usertype = mysql_real_escape_string(addslashes($_REQUEST['user-type']));
}

此外,我尝试以3种方式获取usertype值,但仍然只将驱动程序值插入数据库。

1.$usertype = mysql_real_escape_string($_POST['user-type']);
2.$usertype= isset($_POST['user-type']) ? mysql_real_escape_string($_POST['user-type']) : '';
3.$usertype=$_POST['user-type'];

即使我修复了类似$ usertype ='passenger'的usertype;并插入db值$ usertype .driver插入db 这是竞争对手的php文件

<?php
     include ('../includes/include_files.php');
     $msg = "";


 /* ----ON SUBMIT------ */


if (isset($_POST['submit'])) {

$date = date('Y-m-d');

$full_name = mysql_real_escape_string(addslashes($_REQUEST['fname']));
$email_address = mysql_real_escape_string(addslashes($_REQUEST['email']));
$pass = mysql_real_escape_string(addslashes($_REQUEST['password']));
$contact_no = mysql_real_escape_string(addslashes($_REQUEST['mobile']));
$usertype = 'passenger';
// $usertype = mysql_real_escape_string($_POST['user-type']);
//$usertype= isset($_POST['user-type']) ?    mysql_real_escape_string($_POST['user-type']) : '';
//$usertype=$_POST['user-type'];
// $( "#utype" ).val();
// $("#elementId").val();
if($usertype='driver'){
    $status='Inactive';
}
else
{
    $status="Active";
}

if ($full_name != "" && $email_address != "" && $pass != "" && $contact_no != "" && $usertype != "") {

    $checkinfo = mysql_query("select * from tbl_user where email='" . $_POST['email'] . "' ");
    $count_email = mysql_num_rows($checkinfo);
    if ($count_email == 0) {

        $reg_ins = mysql_query("insert into tbl_user(`fullname`, `email`, `password`, `mobile`, `usertype`,`add_date`, `status` ) values ('$full_name','$email_address','$pass','$contact_no','$usertype','$date', '$status')");

        if ($reg_ins) {

            $uinfo = mysql_query("select * from tbl_user where email='" . $_POST['email'] . "' and password='" . $_POST['password'] . "' ");
            $res = mysql_fetch_object($uinfo);

            echo "<script>window.android.clickOnRegister();</script>";
        }
    } else {

        $msg = " Email ID Already Exist.";
    }
} else {

    $msg = "Please fill all required fields *";
}
}
?>

3 个答案:

答案 0 :(得分:0)

看起来你背后有一些特殊的逻辑。

如果您使用标准方式发送表单,您将获得user-type参数的正确值。

检查表单中是否有唯一的name="user-type"

还要检查firebug / chrome开发人员工具中发送到服务器的值是什么,以确定问题是在后端还是在前端。

答案 1 :(得分:0)

试一试: -

<select size="1" id="slt_country" name="country">
<option value=""> - Select - </option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>

...

<option value="ZM">Zambia</option>
<option value="ZW">Zimbabwe</option>
</select>

可用于获取用户所选国家/地区的Javascript代码:

var selected_index = oForm.elements["country"].selectedIndex;

if(selected_index > 0)
{
   var selected_option_value = oForm.elements["country"].options[selected_index].value;
   var selected_option_text = oForm.elements["country"].options[selected_index].text;
}
else
{
   alert('Please select a country from the drop down list');
}

链路: - http://www.javascript-coder.com/javascript-form/javascript-get-select.phtml

答案 2 :(得分:0)

代码中存在的问题是:

if($usertype='driver'){

因为您只使用一个=而需要像==这样使用双重进行比较。如果一个人等于$usertypedriver。 (也将永远免费)。

所以将该行更改为:

if($usertype=='driver'){