using a variable in $_POST in php? - php

i have a page in which i am using a dropdown menu. i have passed a variable in the name field of the select tag whose value comes from an array.
$query1="Select SSAP from project where projname='$project'";
$result=mysql_query($query1);
while($row=mysql_fetch_assoc($result))
{
$ssap = $row['SSAP'];
$query2="Select * from student where SSAP='$ssap'";
$res=mysql_query($query2);
$row1=mysql_fetch_assoc($res);
$name=$row1['name'];
echo $name; ?> <select name="<?php echo $name;?>">
<option value="A"> Exceptional </option>
<option value="B"> Highly Effective </option>
<option value="C"> Effective </option>
<option value="D"> Good </option>
<option value="E"> Not Satisfactory </option>
</select> <br> <?php
}
and i need to retrive the value of each select tag created in another variable on the action page.
$grade=$_POST[$name];
echo $grade;
the first code snippet works fine but i am unable to fetch the value in the second snippet.

When the form containing the <select> with the dynamic name is submitted, it is a different request, so $name hasn't been set.

When your form is submitted, the <select> names are getting evaluated like <select name="Raj">, <select name="Kiran">, ..........<select name="Sameer"> etc depending on the values of $name=$row1['name']; from your query on 'student' table. Also you should pay consideration to the fact that your query $query2="Select * from student where SSAP='$ssap'"; might return more than one rows, it seems you are assuming only one row will be returned.
Also note $_POST is an associative array that accept parameters as
KEY=>VALUE pairs from submission of HTML. In $_POST key is name of
element in input form value is value of that element in input form
So you can not use a variable as the KEY in your SUPER GLOBAL $POST like this $_POST[$name]; when $name is undefined on your action script. I would suggest you use an named array in your multiple <Select>. Do a var_dump($_POST) on your action script. That may reveal most of the stuffs for you.

Related

PHP variable in header function in one drop list with two values [duplicate]

I'd like to post two values in one drop down option and I'm not sure about the best way to approach it.
The drop down list pulls data in from an external service. This data is 'id' and 'name'.
When I select an option in the drop down and then press submit I want the 'id' and the 'name' to be posted.
My code looks like this:
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
I've tried putting in a hidden input field, but that then doesn't render out the drop down (instead it just creates a list).
I am using both the id and name elsewhere, so I don't want to have to post just the id and then have to get the name again, hence I want to post both of them at the same time.
Any recommendations?
You cannot post two values unless both of them appear in the value attribute. You can place both in, separated by a comma or hyphen and explode() them apart in PHP:
// Place both values into the value attribute, separated by "-"
<option value="<?php echo $name['id'] . "-" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
Receiving them in PHP
// split the contents of $_POST['data'] on a hyphen, returning at most two items
list($data_id, $data_name) = explode("-", $_POST['data'], 2);
echo "id: $data_id, name: $data_name";
You may add a hidden field with the name "id" and then bind an onchange event listener to the <select>. inside the onchange function, get the value of the <select> and assign it to the "id" field.
<form>
<select name="name" onchange="document.getElementById('id').value=this.value">
<!--
...
options
...
-->
</select>
<input type="hidden" name="id" id="id" />
<input type="submit" />
</form>
You could output something like:
<option value="<?php echo $name['id']."_".$name['name'];?>">
Then use preg_splitor explode to separate the two once the form data is processed.
The only way to do this is to include both pieces of information in the value for the single option. Just use code like the following, and then when you get the value back, split the string at the first underscore to separate out the values.
<select name="data">
<option>Select a name</option>
<?php foreach($names as $name): ?>
<option value="<?php echo $name['id'] . "_" . $name['name']);?>">
<?php echo $name['name']);?>
</option>
<?php endforeach; ?>
</select>
Also, you could just leave the ID alone in your form, and then look up the id again when the user submits the form.
You could make as many hidden inputs as there are options, each with the name of one option value. The input's values are the contents of the options. In the second script, you can look for the dropdown value, and then take the value of the hidden input with that name.
We can pass it with data attribute, and can access in js,
<option value="<?php echo $name['id']);?>" data-name="<?php echo $name['name']);?>">One</OPTION>
var name = $(this).find(':selected').data('name');

HTML option selects first default value instead of choosen value

Whenever I Select any other options in my drop down I am still getting the first value which is '?' instead of any of the other ones I picked.
<select name="role" class="form-control">
<option value="?" label ="Please Select a Role" id="role">Select a Role</option>
<?PHP foreach($roles as $role){ ?>
<option value= "<?=$role['id']?>" label=" <?=$role['role']?>"><?=$role['role']?>
</option>
<?PHP } ?>
</select>
What am I missing?
I made a mistake
The reason I was not getting getting any other values was because I was getting the ID of the first option so it would retrieve the same value each time
I solved it by moving the value of Id to the first select tag
My best guess is that you are pulling the value off whatever DOM element has an id of role. In this case, you've defined an option that always has the id role.

php, get data from HTML select options, store them in variables and perform something

first, i've got a select option in a HTML document
<form action="Journey.php" method="post">
<select name = "Startpoint">
<optgroup label = "Start point">
<option value = "GrimesDyke">GrimesDyke</option>
<option value = "SeacroftRingRoad">SeacroftRingRoad</option>
<option value = "WykeBeck">WykeBeck</option>
<option value = "FfordeGrene">FfordeGrene</option>
<option value = "St.JamesHospital">St.JamesHospital</option>
.........
i determine the action is to pass the data using post method to Journey.php file in order to perform some algorithm, but when i click on the submit button in the browser it shown me my entire php code.... so i decided to run a few tests like this:
Journey.php
<?php
if(isset($_POST['submit'])){
$selected = $_POST['Startpoint']; // Storing Selected Value In Variable
echo "You have selected :" .$selected; // Displaying Selected Value
}
?>
this time, it displayed nothing, what i'm trying to do is, to store the Startpoint's value passed to the php file in a $selected variable and echo it on the screen, but it's still not working
i checked many examples online but honestly i can't see what i did wrong, please point out my mistake and show me how exactly i can make it right, thank you very much.
I wrote on php long time ago, but as I remember there is no 'submit' key in the $_POST table. Try to check for 'Startpoint' key instead.
This works for me:
<?php
if(isset($_POST['submit'])){
$selected = $_POST['startpoint']; // Storing Selected Value In Variable
echo "You have selected: " . $selected; // Displaying Selected Value
};
?>
<form action="" method="post">
<select name = "startpoint">
<optgroup label = "Start point">
<option value = "GrimesDyke">GrimesDyke</option>
<option value = "SeacroftRingRoad">SeacroftRingRoad</option>
<option value = "WykeBeck">WykeBeck</option>
<option value = "FfordeGrene">FfordeGrene</option>
<option value = "St.JamesHospital">St.JamesHospital</option>
<input type="submit" name="submit" value="Submit">
</form>

retrieving value of select after posting form

The select tag is being generated from the database through PHP echo command.
After I post the form including my select element, I want to be able to retrieve the content (between option tag) as well as the value in the option tag. So if I chose Volvo and submitted the form. I want to retrieve Volvo and 1.
<select name='car'>
<option value='1'>Volvo</option>
<option value='2>Saab</option>
<option value='3>Mercede</option>
<option value='4>Audi</option>
</select>
This will only retrieve the value.
$_POST['car']
how can I retrieve both?
You can't. But you know that 1 is always Volvo.
Just keep it in an array:
$cars = array('Volvo', 'Saab,' 'Mercede', 'Audi');
$car_name = $cars[$_POST['car']];
This way you can also generate the select from the array:
<select name='car'>
<? foreach ($cars as $key => $car_name): ?>
<option value='<?= $key ?>'><?= $car_name ?></option>
<? endforeach; ?>
</select>
you can get like this
<select name='car'>
<option value='1,Volvo'>Volvo</option>
</select>
and
$value = explode(",",$_POST['car']);
you get
$value[0]= 1
$value[1]= Volvo
Unless you're using jQuery to manipulate your submission, you can just modify the value attribute to include the same word as the content of the option. You wouldn't be able to submit POST data extracted from the content of the option, though.

Can I pass Ajax var result to PHP code?

Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
Greetings.
I am coding a basic form that uses a SELECT list I populate from my database.
However, my user needs the form to be dynamic and wants to be able to select either MasterTable1 or MasterTable2 or MasterTable3...
Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity...
I can output <div id='txtHint'></div> in my page and it shows the correct table name that was picked.
But how do I pass the correct table name to my query that will populate my SELECT list???
I tried
<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>
<?php
$sql="select distinct filename from "."<div id='txtHint'></div>";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option>
<?php } ?>
</select>
But to no avail. This is confusing since I can do this...
...
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gettable.php?q="+str,true);
xmlhttp.send();
}
</script></head>
<body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)">
<option value="">Select Data Table</option>
<option value=""> </option>
<option value="MasterTable1">up to 31 days old</option>
<option value="MasterTable2">62 days old</option>
</select>
</form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div>
</body></html>
And the name of my table will be displayed in the SELECT list 'tablist'.
How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!
(Pastebin =>form code)
m8, ajax is mainly used for user experience and populating a select list is so easy mode that it shouldnt be bothered with ajax in the first place!
You should use ajax if you want to use some methods on user submitted data and create an illusion of seamless data exchange between the server and the client, and based on the return results render a corresponding view or an element of the view.
unless you load every element of the view with ajax, you should populate your html with php from the start!
<select name="DBFilename" id="DBFilename" size="whatever since style belongs to css">
<option selected="selected">Select Filename</option>
<?php
$sql="SELECT DISTINCT filename from 'wherever'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename'];?>
</option>
<?php } ?>
</select>
Create a separate php script which returns a list of select options -> values depending on the table name given to it. You must remember to protect against sql injection. Then use ajax to retrieve the list and insert it into the select.

Categories