The issue is i can only retrieve one value from the select dropdown even i have selected 2, have tried to look at similar question here,but none seems to work for me.
Any thoughts? Thanks
if (isset($_POST['submit'])){
$smsorcall = $_POST['smsorcall'];
foreach($smsorcall AS $index => $smsorcall ) {
echo "$smsorcall";}
}
<form action="newpatient.php" method="post">
<p>Reminder Preference: *</p>
<select name="smsorcall[]" style="width: 250px" class="form-control" multiple>
<option value="SMS">SMS</option>
<option value="Call">Call</option>
<option value="Email">Email</option>
</select>
<button type="submit" name="submit">Submit</button>
</form>
My code
<?php ob_start();
session_start();
include('connect-db.php');
if (isset($_POST['submit']))
{
$patientid = $_POST['patientid'];
$smsorcall = $_POST['smsorcall'];
foreach($smsorcall AS $index => $smsorcall ) {
echo "$smsorcall";}
$_SESSION['smsorcall'] = $smsorcall;
In another html page, i echo the $_SESSION['smsorcall'] to display the result
You are only saving the last value to the session - you should save the whole array from the POST, then when you want to get the values loop through the array from the session, e.g.:
if (isset($_POST['submit']))
{
// save the WHOLE ARRAY of selected options to the session
$_SESSION['smsorcall'] = $_POST['smsorcall'];
/* Any more code here... */
}
On your other page:
if (isset($_SESSION['smsorcall']))
{
// Get the array of all selected options from the session
$smsorcall = $_SESSION['smsorcall'];
// loop through the array to process each option one at a time
foreach($smsorcall AS $index => $option ) {
// Do whatever you want with each option here, e.g. to display it:
echo $option;
}
}
Related
Once a form is submitted I need to search through the POST array for words like 'ProductID' and store the name and the value in an associative array. I need to do this as you can list x amount of products through another page. So sometimes there's only 3 products listed, other times it's 10 and so on.
The HTML:
<div class="col-sm-3">
<label class="control-label">ADULT TRIP - $139</label>
<select class="form-control valid" name="ProductID-1">
<option value="-1">-- Please Select --</option>
<option value="0">0</option><option value="1">1</option>
<option value="2">2</option><option value="3">3</option>
</select>
</div>
What I've got so far in PHP:
foreach( $_POST as $value ) {
if (strpos($_POST,'ProductID') !== false) {
//Store in associative array
}
}
What I want to achieve is, once the form is submitted, loop through POST, check each item for the word "ProductID", if there's a match, store the name and value into an associative array. At the moment everything is returning true. Any help would be greatly appreciated.
Use:
foreach( $_POST as $key=>$value ) {
if (strpos($key,'ProductID') !== false) {
//Store in associative array
}
}
Instead:
foreach( $_POST as $value ) {
if (strpos($_POST,'ProductID') !== false) {
//Store in associative array
}
}
If you will have several ProductID I can use an array name="ProductID[1]" it will be easy then to manage your post
$ProductID= $_POST['ProductID'];
try echo $ProductID[0];
As pointed out by Ôrel I can use an array and loop through that as I see fit. I changed the HTML from:
<select class="form-control valid" name="ProductID-1">
to:
<select class="form-control valid" name="ProductID[<? echo $product_Results['ProductID']; ?>]">
Upon submitting the form, I changed my PHP to:
foreach( $_POST as $stuff ) {
if( is_array( $stuff ) ) {
foreach( $stuff as $ProductID => $qty ) {
echo 'product ID: '.$ProductID.' - qty: '.$qty.'<br />';
}
}
}
That way I can loop through the form until I find one that is the array (ProductID) and loop through that as an associative array. This allows me to get the Product ID as well as the quantity to go onto the next step.
I need to store all the items selected from a dropdown box inside array. I have only one form now with the select dropdown list. So each time I select an item from the list and submit the form it overwrites the previous item.
How do I make it work like each time it submits the id will be stored so that I can display all the items selected?
//this is the select dropdown for addon item
<select name="addon">
<?php
mysql_select_db($database_bumi_conn, $bumi_conn);
$query="SELECT * FROM tbl_addons WHERE status=1";
$result=mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$a_id=$row['addOns_id'];
$a=$row['addOns'];
?>
<option value="<?php echo $a_id?>"><?php echo $a;?></option>
<?php
}
?>
</select>
//And this is how I store the id
$addon_id=$_POST['addon'];
//edited with session
$_SESSION['option']=array();
$_SESSION['option'][$addon_id]=array('qty'=>$qty,'date_1'=>$date_1,'date_2'=>$date_2);
print_r($_SESSION['option']);
foreach($_SESSION['option'] as $option=>$value)
{
echo $option.'=>';
foreach($value as $val)
{
echo $val;
}
}
You could use SESSION' variables to store all the ID :
session_start();
// Rest of your code here
// $addon_id=$_POST['addon']; Becomes :
if (!in_array($_POST['addon'], $_SESSION['addons']))
$_SESSION['addons'][] = $_POST['addon'];
Edit: Not sure about your edit with sessions. You're resetting $_SESSION['option'] everytime, losing previous addon_id values
Ignoring the fact that you're using a deprecated, inherently un-secure and unmaintained extension, you need to use the multiple attribute on your <select> element and let PHP know that it will be receiving an array of values by using the [] suffix on the element name. To summarise...
<select name="addon[]" multiple>
<?php foreach($collection as $val => $label) : ?>
<option value="<?= htmlspecialchars($val) ?>"><?= htmlspecialchars($label) ?></option>
<?php endforeach ?>
</select>
The PHP variable $_POST['addon'] will then contain an array of selected values (when the form is posted of course).
I have code that basically uploads a spreadsheet of data, could have 5 columns or 10 columns or whatever. Each column needs to have a drop down box specifying the data type, and the data types selected should be submitted via post with a form to another php site. How would I do this, this is the code that creates the elements.Is there a way to dynamically create elements, like delimist . attCount, and how would i put this loop into an html form to submit the data? Is there any way to put the values of all of these into an array for convince? Thanks.
EDIT: Ok thanks for the dynamic stuff, but how would i put this into a form and submit this via post to another page. Or is there another way to post information. I dont know much about php.
while($attCount < count($attArray)) {
echo "<select name=\"delimList\">";
echo "<option value=1>tab</option>";
echo "<option value=2>comma</option>";
echo "<option value=3>semi-colon</option>";
echo "<option value=4>ampersand</option>";
echo "<option value=5>pipe</option>";
echo "</select>";
$attCount = $attCount + 1;
}
Using a foreach loop and assigning the first index in your array will give you what you're expecting:
$options = [1=>'tab','comma','semi-colon','amperstand','pipe','another-value'];
echo "<select name=\"delimList[]\">\r\n";
foreach($options as $val => $option){
echo "<option value=\"$val\">$option</option>\r\n";
}
echo "</select>\r\n";
output:
<select name="delimList[]">
<option value="1">tab</option>
<option value="2">comma</option>
<option value="3">semi-colon</option>
<option value="4">amperstand</option>
<option value="5">pipe</option>
<option value="6">another-value</option>
</select>
To sumbit the page you'll need to put everything inside of
<form method="post"> // you may want to add an action="..."
.... // if you want to post to another page,
</form> // instead of the same one
You'll also need a submit button too...
<input type="submit" name="submit" value="submit" />
After submit you can check the values like this:
if(isset($_POST['delimList'])){
foreach($_POST['delimList'] as $key => $value){
echo "delimList[$key] = $value";
}
}
You can put all the value of all of these into an array… by putting all the values of these into an array!
$myArray = ['tab','comma','semi-colon','amperstand','pipe','another-value'];
echo '<select name="delimList">';
for ($i = 0; $i < count($myArray); $i++) {//Loop thru all
echo '<option value="'.$i.'">'.$myArray[$i].'</option>';
}
echo "</select>";
This gives lists all people in the db and give a drop down for each one of them i want to make it so when i hit one submit button it enters individual values for each person.
so if you make yes for bobby no for mark and yes for dustin you can the pres submit and it will enter that for there values
$results = mysql_query("SELECT * FROM `$tabeluser` WHERE buss='$buss' ORDER BY id DESC LIMIT 1");
while($rows = mysql_fetch_array($results) )
{
fn = $_POST['firstname'];
echo $fn;
?>
<form>
<select name="check">
<option>no</option>
<option>yes</option>
</select>
<?php
<input type="submit" name="submit">
?>
<form>
<?php
}
mysql_query("INSERT INTO `$fn` (buss) VALUES ('$_POST[check]')");
First of all, you create a <form> and a submit button for each of the records you have. That is wrong, since you want to update multiple values at once.
What it should look like would be:
<form>
<?php
while($rows = mysql_fetch_array($results)) {
print '<select name="check[]"> .. </select>';
}
?>
<input type="submit" name="submit" />
</form>
Secondly, your code is formatted as if you are expecting to get $_POST[check] right after sending the code to the browser. That is not how PHP works. You need to separate the logic of having posted values, before printing the actual page contents. PHP is server side, which means that it won't get any data, unless the script is called with it from the beginning. So, that should look something like:
<?php
if (isset($_POST["check"])) {
// handle posted data.
}
else {
// show form
}
// or show form here, without an else, if you want to always show a form,
// even when you have posted values.
?>
Last but not least, you need to find a way to know which posted value belongs to each of your records. The way you have it now (<select name="check">') it will just return you one single value.
If you write it the way I intentionally did above (`) you will get all values, but still you won't be able to easily recognize which value is for each record.
Instead, you may want to do a final result of something like:
<?php
// get mysql records into an array (say $my_array)
if (isset($_POST["submit"])) {
foreach($my_array as $record) {
if (isset($_POST["select_of_id_".$record["id"])) {
// insert additional value into db
}
}
}
print '<form>';
foreach($my_array as $record) {
print '<select name="select_of_id_'.$record["id"].'">';
print '<option value="0">no</option><option value="1">yes</option>';
print '</select>';
}
print '<input type="submit" name="submit"/>';
print '</form>';
?>
Changes required in your code :-
<select name="check[]">
<option value="<?php echo $userId;?>_0">no</option>
<option value="<?php echo $userId;?>_1">yes</option>
</select>
You should make changes in you DB It help to easy maintaing your data.
Create new table where you can save multiple user check data with respective Post
for e.g post_id user_id check
101 111 0
101 112 1
How you can store data from you html
In you post array you will get check array in which you will get multiple check value like this
101_0 if no selected and 102_1 if yes selected. Now you need to explode this value.
$_POST['check'] = array(0 => `101_0`, 1 => 102_1);
Inside loop you can extract userid and respective checked value.
for e.g
$checked = explode('_','101_0');
$userId = $checked[0];
$check = $checked[1];
Let's say I have a photo upload system where the user have to set a category for each album to get the basics for a nice and clean search functionality. But if an user is changing now a value like this:
<select>
<option value="">Choose a Category</option>
<option value="Holiday">Holiday</option>
</select>
to this:
<select>
<option value="">Choose a Category</option>
<option value="Holiday">Something Stupid</option>
</select>
is "something stupid" entered into the database.
That's why I have to do a server side check. But I don't know how to get all the correct values of the option fields to compare it with the posted value.
So my first considerations were the following:
PHP:
// Get all values of the option fields
// Push all the values into an array
if (in_array('foo', $array)) {
// foo is in the array
}
Thank you for helping.
Ok, so I think I guessed what you tried to tell.
You should not have the tags hardcoded in your list.php file, but instead have an array there. That way, you can use it both for generating the select field, but also for the verification. However, generally a database table with the categories would be preferable.
path/list.php
return array(
'1' => 'Name of Ctg 1',
'2' => 'Name of Ctg 2'
);
Here you generate the select
<select name="whatever">
<?php
$options = include('path/list.php');
foreach($options as $id => $name) {
echo '<option value="' . $id . '">' . $name . '</option>';
}
?>
</select>
And how to verify it then in the "target" page
$options = include('path/list.php');
if(!array_key_exists( $valueFromUser, $options ) ) {
// invalid option
}
When the data is posted from the page containing the select tag, it will be in the $_REQUEST array, if you run this php in catcher php page:
foreach ($_REQUEST AS $key => $value) echo "$key = $value <br>";
you will see the results from your list.php.