Hadi Samadbin Hadi Samadbin - 24 days ago 9
Javascript Question

Geting the value of selecting dropdown

Trying to get the value of selecting dropdown as below

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


but always the driver value is inseted into db even passenger is selected by user

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']));
}


Also I tried to get usertype value in below 3 ways but still only driver value is inserted into DB.

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'];


even when I fix the usertype like $usertype = 'passenger'; and insert into db value $usertype .driver is inserted into db
here is the compelet php file

<?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 *";
}
}
?>

Answer

The problem in your code is at this line:

if($usertype='driver'){

Because you use only one = and you need to use double like this == to do a comparison. With one equal you asing to $usertype the value driver. (also it will always be free).

So change that line to:

if($usertype=='driver'){