Search $_POST array using strpos() - php

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.

Related

Use an array as select form

I am a beginner and I have written a piece of code to use an array values inside a select form, but I don't know how to get the chosen value.
What I have :
$list = '<select><option>' .implode('</option><option>',$apn).'</option></select>';
Now I would like to get the name of the chosen value but I don't know how.I tried to add
<select name="test">
to my select form and get it with
echo $_POST['test']
but it does not work.
Thank you !
<?php $myArray = array(1 => 'red', 2 => 'blue'); ?>
<select name="theName">
<?php
foreach ($myArray as $key => $value) { ?>
<option value="<?php $key ?>"><?php echo $value ?></option>
<?php } ?>
</select>
Basically, you are looping through the option value with a foreach statement and assigning the key as the value.
You then get the chosen value with name 'theName'... echo $_POST['theName'];

Fail to retrieve multiple values from select dropdown in PHP

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

Get all values of option fields and push it into an array

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.

Add dynamic value to a hidden form field

I'm having a contact form field named, addons with select options
it's code is
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_name; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
Every addon have it's unique price. It can be fetch through $options->addon_price; I added only addon name in contact form so i'm getting only it's name once user submits form. Likewise, I need to get that addon price too without creating new field and asking users to select price in dropdown.
Any ideas to do this ?
You can use the following trick:
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_name.','.$options->addon_price; ?>">
<h5><?php echo $options->addon_name; ?></h5>
</option>
<?php } ?>
Add the price with the name in value of dropdown and then after submitting the page you can get the string with name and price and then you can split it with the , by using the explode function like it:
$pieces = explode(",", $str);
And you can use the price and name by $name = $pieces[0] and $price = $pieces[1].
You Can use
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_price; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
and u can fetch the price form the drop down
you can try this you will be able to get the price and it will show addon_name to the user.
<?php foreach ($addons as $options) { ?>
<option value="<?php echo $options->addon_price; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
It sounds like you need a unique ID to identify the addons. If it's a static array that doesn't change, you can even use the array keys for that purpose, but I would add an addon_id field to the objects.
The addons array would be something like this:
$addons = array(
1 => $addon1,
2 => $addon2,
3 => $addon3,
... etc
);
Your select options can have the array keys as values:
<?php foreach ($addons as $key => $options) { ?>
<option value="<?php echo $key; ?>"><h5><?php echo $options->addon_name; ?></h5></option>
<?php } ?>
Then when you're processing the selected addon, use the key to access the appropriate addon, like so:
$selected_addon = $addons[$_POST['addon']];
, obviously substituting the correct form element name, and sanitizing the input first, this is just to give you an idea.

How to compare a value selected from the drop down box with the array elements?

I have a php file with 2 arrays in it
//script containing all the rublics and subrublics
$rublic[0]='rublic1';
$rublic[1]='rublic2';
$rublic[2]='rublic3';
$rublic[3]='rublic4';
$rublic[4]='rublic5';
$subrublic[0]='subrublic1';
$subrublic[1]='subrublic2';
$subrublic[2]='subrublic3';
$subrublic[3]='subrublic4';
$subrublic[4]='subrublic5';
?>
The elements of these arrays are shown in the drop-down box. What I need to do is to grab the element which the user chose from the box and write the index number of the choice selected into a database field. How could I do that?
I will post my code here even though I realize that my approach to this problem is completely wrong from the start :(
//add the index number of the rublic and the subrublic to the db
include('rublics.php');
if(isset($_POST[' article_type']) && ($_POST['article_type'] != '0')){
$rublic_selected = $_POST['article_type'];
for($count_rublic=0; $count_rublic<=10; $count_rublic++){
if($rublic_selected == $rublic[$count_rublic]) {
$rublic_selected = $count_rublic;
}
if($rublic_selected == $subrublic[$count_rublic]){
$rublic_selected = $count_rublic;
}
}
} else {
echo 'You did not make the selection. Please choose the type of the article.';
}
Your dropdown can/should use the index numbers for the value attribute on the option elements. Ie:
<select id="article_type" name="article_type">
<option value="0">rublic1</option>
<option value="1">rublic2</option>
<option value="2">rublic3</option>
<option value="3">rublic4</option>
<option value="4">rublic5</option>
</select>
Then when the form is POSTed to your PHP script, you will already have the correct index number to write to the database.
Try this, it should be a good start:
$rublic[0]='rublic1';
$rublic[1]='rublic2';
$rublic[2]='rublic3';
$rublic[3]='rublic4';
$rublic[4]='rublic5';
$subrublic[0]='subrublic1';
$subrublic[1]='subrublic2';
$subrublic[2]='subrublic3';
$subrublic[3]='subrublic4';
$subrublic[4]='subrublic5';
//$user_input = 'rublic3';
$user_input = 'subrublic4';
$options = array('rublic', 'subrublic');
foreach($options as $option_key => $option_value)
{
foreach($$option_value as $key => $value)
{
if($value == $user_input)
{
echo 'found it at ', $option_value, ' ', $key;
break;
}
}
}
Basically, I loop through both arrays until I find the string that matches the correct user input and return the name of the array it was found in (rublic or subrublic) plus the index of that array.

Categories