Get the Selected Index Value of <Select> Tag in PHP

get the selected index value of select tag in php

Your form is valid. Only thing that comes to my mind is, after seeing your full html, is that you're passing your "default" value (which is not set!) instead of selecting something.
Try as suggested by @Vina in the comment, i.e. giving it a selected option, or writing a default value

<select name="gender">
<option value="default">Select </option>
<option value="male"> Male </option>
<option value="female"> Female </option>
</select>

OR

<select name="gender">
<option value="male" selected="selected"> Male </option>
<option value="female"> Female </option>
</select>

When you get your $_POST vars, check for them being set; you can assign a default value, or just an empty string in case they're not there.

Most important thing, AVOID SQL INJECTIONS:

//....
$fname = isset($_POST["fname"]) ? mysql_real_escape_string($_POST['fname']) : '';
$lname = isset($_POST['lname']) ? mysql_real_escape_string($_POST['lname']) : '';
$email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : '';
you might also want to validate e-mail:
if($mail = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
$email = mysql_real_escape_string($_POST['email']);
}
else
{
//die ('invalid email address');
// or whatever, a default value? $email = '';
}
$paswod = isset($_POST["paswod"]) ? mysql_real_escape_string($_POST['paswod']) : '';
$gender = isset($_POST['gender']) ? mysql_real_escape_string($_POST['gender']) : '';

$query = mysql_query("SELECT Email FROM users WHERE Email = '".$email."')";
if(mysql_num_rows($query)> 0)
{
echo 'userid is already there';
}
else
{
$sql = "INSERT INTO users (FirstName, LastName, Email, Password, Gender)
VALUES ('".$fname."','".$lname."','".$email."','".paswod."','".$gender."')";
$res = mysql_query($sql) or die('Error:'.mysql_error());
echo 'created';

How to get selected index of dropdown from PHP post

I am not sure why you can't use the value attribute - the descriptive string would be the text portion of the option element, the filename to save could be the value:

<option value="path/to/file_to_save.php">Descriptive file name</option>

Doing it that way, the user sees the descriptive text, the server gets a useful bit of information it needs when the form posts.

If that is not an option, you could add an onSubmit event to the form in which you pass the selectedIndex property to a hidden form field, then return true and let the form submit normally.

Form snippet

<form onsubmit="return beforeSubmit()">
<input type="hidden" name="file_index" value="" id="file_index_fld" />
<select id="file_name_dropdown">
<option>...</option>

Javascript snippet

var beforeSubmit = function () {
$('#file_index_fld').val($('#file_name_dropdown').attr("selectedIndex"));
return true;
}

... now in PHP's $_POST variable, you'll see $_POST['file_index'] contains the selectedIndex of the select element.

The long and short of it is that the selectedIndex property is a DOM item and not part of the POST data. No matter what, you are either going to have to intervene with javascript to add the data to POST, or modify your option elements to pass the desired data. I would always lean toward the former route as it is less complex.

HTML & PHP - get index of selected value in dropdown list

You could iterate through the values and count, what you get ... i do not think, this is a good way. I do not think, there is a quicker, easier method to do this but using the "value" attributes.

<option value="1">First option</option>
<option value="2">Second option</option>
<option value="3">Third option</option>
...

Using $_POST to get select option value from HTML

Use this way:

$selectOption = $_POST['taskOption'];

But it is always better to give values to your <option> tags.

<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>

Setting the selectedindex property of an HTML dropdown using PHP

selectedindex property is not used in this way, for appropriate use of the selectedindex property you can view http://www.w3schools.com/jsref/prop_select_selectedindex.asp.

Instead what you are after is applying the "selected" attribute to the appropriate option. The code below runs some shorthand if/else checks and will apply the "selected" attribute where appropriate, based on your $genderIndex you created in your function given.

<select name="gender">
<option value="0" <?php echo ($genderIndex==0)?('selected'):(''); ?>"> </option>
<option value="Male" <?php echo ($genderIndex==1)?('selected'):(''); ?>>Male</option>
<option value="Female" <?php echo ($genderIndex==2)?('selected'):(''); ?>>Female</option>
<option value="Other" <?php echo ($genderIndex==3)?('selected'):(''); ?>>Other</option>
</select><br/>

How to set selected value of HTML select box with PHP

The manual way.....

<select name="interest">
<option value="seo"<?php if($result['interest'] == 'seo'): ?> selected="selected"<?php endif; ?>>SEO и Блоговодство</option>
.....

The better way would be to loop through the interests

$interests = array(
'seo' => 'SEO и Блоговодство',
'auto' => 'Авто',
....
);

<select name="interest">
<?php foreach( $interests as $var => $interest ): ?>
<option value="<?php echo $var ?>"<?php if( $var == $result['interest'] ): ?> selected="selected"<?php endif; ?>><?php echo $interest ?></option>
<?php endforeach; ?>
</select>

how to get value from select tag in javascript

Add id to your select

<select name="dropdown" onchange="myFunction()" id="dropdown">
^ ^

You are fetching the value based on id in script, but the id doesnot exists.

Here is one more best solution

<select name="dropdown" onchange="myFunction(this.value)">
<option value="1">1</option>
<option value="2">2</option>
</select>

In script

function myFunction(val){    
alert(val)
}

Fiddle demo

How do I set the selected item in a drop down box

You need to set the selected attribute of the correct option tag:

<option value="January" selected="selected">January</option>

Your PHP would look something like this:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

I usually find it neater to create an array of values and loop through that to create a dropdown.



Related Topics



Leave a reply



Submit