I have an issue getting variables using PHP in a tablet site I have been working on. I have a search form on the homepage that passes variables to a list page. I then have the same search form in a 'refine search' dialog box which should pre-select the appropriate values depending on what has been passed, using PHP.
The problem is I can't seem to get the variables that have been passed (using PHP). For example I have this field in my search:
<select name="propertyType" id="propertyType">
<option value="">Any Type</option>
<option value="1"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="1") { echo ' selected'; } ?>>Houses</option>
<option value="2"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="2") { echo ' selected'; } ?>>Flats/Apartments</option>
<option value="3"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="3") { echo ' selected'; } ?>>Bungalows</option>
<option value="4"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="4") { echo ' selected'; } ?>>Other</option>
</select>
But when I pass any of these values my code does not pick them up and echo the relevant 'selected'.
The tablet site can be found here: http://muskermcintyre.co.uk/tablet Any help would be much appreciated!
Thanks.
I suggest you following syntax:
<?php
$values = array(
0 => 'Any Type',
1 => 'Houses',
2 => 'Flats/Apartments',
3 => 'Bungalows',
4 => 'Other'
);
$current = (int) $_GET['propertyType'];
?>
<select name="propertyType" id="propertyType">
<?php
foreach ( $values as $key => $value ) {
$selected = $key == $current ? 'selected="selected"' : '';
echo "<option value='$key' $selected >$value</option>";
}
?>
</select>
I've had a similar problem in the past and got round it by getting the query string using Javascript/jQuery.
A quick search gave me this which might help you if you go down a similar route:
How can I get query string values in JavaScript?
Related
I wanted to have a dropdown's selected value be determined by the results of the a query. But whatever I try, its selected value stays at the very first option.
I've browsed multiple questions with the same problem, but none solved my problem.
I've tried this:
<select name="Period">
<option value="Day" <?php if($PeriodTXT == "Day") echo "selected"?>>Day</option>
<option value="Week" <?php if($PeriodTXT == "Week") echo "selected"?>>Week</option>
<option value="Month" <?php if($PeriodTXT == "Month") echo "selected"?>>Month</option>
<option value="Year" <?php if($PeriodTXT == "Year") echo "selected"?>>Year</option>
</select>
And this:
<select name="Period">
<option <?php echo ($PeriodTXT == 'Day')?"selected":"" ?> >Day</option>
<option <?php echo ($PeriodTXT == 'Week')?"selected":"" ?> >Week</option>
<option <?php echo ($PeriodTXT == 'Month')?"selected":"" ?> >Month</option>
<option <?php echo ($PeriodTXT == 'Year')?"selected":"" ?> >Year</option>
</select>
But it won't work. Also when I use echo "$PeriodTXT"; it echos "Week" (exactly as written in the options of the dropdown), so it should've selected "Week" but it doesn't.
EDIT: $PeriodTXT is supposed to show the selected interval that came with a number count (example "3 Day" it would only keep the "Day" part)
$usersdata = array();
while($row = mysqli_fetch_array($result))
{
$usersdata = $row;
}
$PeriodTXT = preg_replace("/\d+/u", "", $usersdata[4]);
Your problem was extra whitespace around the text while you were removing count with regex, so hidden extra whitespace was yielding condition as false,
$PeriodTXT = '3 Week';
$PeriodTXT = preg_replace("/\d+/u", "", $usersdata[4]); //without trim()
var_dump($PeriodTXT); // outputs string ' Week' (length=5)
but when we trim it
$PeriodTXT = trim(preg_replace("/\d+/u", "", $usersdata[4])); //with trim()
var_dump($PeriodTXT); // outputs string 'Week' (length=4)
So use trim() to fix that, working eg,
<?php
$usersdata = array();
$usersdata[4] = '3 Week';
$PeriodTXT = trim(preg_replace("/\d+/u", "", $usersdata[4]));
?>
<select name="Period">
<option <?php echo ($PeriodTXT === 'Day')?"selected ='selected'":"" ?> >Day</option>
<option <?php echo ($PeriodTXT === 'Week')?"selected ='selected'":"" ?> >Week</option>
<option <?php echo ($PeriodTXT === 'Month')?"selected ='selected'":"" ?> >Month</option>
<option <?php echo ($PeriodTXT === 'Year')?"selected ='selected'":"" ?> >Year</option>
</select>
Also notice I have changed the HTML part from "selected":"" to "selected ='selected'":""
I understand you send a form using POST method, and after you send it, you would like to try display it with the selected option.
Please describe what is $PeriodTXT variable and how do you set it up.
Here is a solution to make it easy, hope it will help you.
$sent = $_POST['Period'];
$options = aray("Day", "Week", "Month", "Year");
echo '<form method="post" action="">
<select name="Period">';
foreach ($options as $v)
{
$selected = ($sent == $v) ? ' selected' : null;
echo '<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
}
echo '</select>
<input type="submit" value="send">
</form>';
EDIT:
Code with "while" you have posted, seems to be wrong.
You are creating $userdata variable each time when while() goes. In fact it's not a real array, as you want it to be. It only works, when you select only one record from database (when mysql_num_rows($result) == 1). Otherwise it' wrong.
It should be done this way:
$usersdata = array();
while($row = mysqli_fetch_array($result))
{
$usersdata[] = $row;
}
$PeriodTXT = preg_replace("/\d+/u", "", $usersdata[$index][4]);
Notice, that you have to know which row ($index variable) you are doing changes in.
$usersdata becomes an Associative array.
Use var_dump() function to check what a variable contains itself. I hope it will get you closer to the solution. If you need more help, you have to post more code, because for now not everything is clear here.
when I'm displaying a user info, it has roles. God and Pesant. The code looks like this:
<td>
<select name='' id='' class='form-control'>
<option value='god'>God</option>
<option value = 'pesant'>Pesant</option>
</select>
</td>
When i'm displaying this info, how can I call from MySQL DB to show the value that is assigned to it? Right now it will always show God to everyone, and if I press Save, it saves the admin aka God mode to everyone.
Thanks. :)
You can check the values that you were getting from database as
<option value='god' <?php echo ($value === 'god') ? 'selected' : ''; ?>>God</option>
<option value ='pesant' <?php echo ($value === 'pesant') ? 'selected' : ''; ?>>Pesant</option>
This'll check that the values that you were getting from database matches with option value if yes then it'll place selected attribute else ''
Check & add selected attribute
<option value='god' <?php echo ($value == 'god') ? 'selected' : ''; ?>>God</option>
<option value ='pesant' <?php echo ($value == 'pesant') ? 'selected' : ''; ?>>Pesant</option>
Assuming $value will contain the value from database. If have run a query and fetch the value from database.
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.
What would be the best way to set tag selected value?
<select id="attr_field_10" name="config_eph_payment_type">
<option value="5">First</option>
<option value="1">Second</option>
<option value="2">Third</option>
<option value="3">Fourth</option>
<option value="4">Fifth</option>
<option value="7">Sixth</option>
</select>
The value comes from $config_eph_payment_type variable (5, 1, 2, 3, 4 or 7).
Something like this should work:
$options = array(
5 => "First",
1 => "Second",
...
7 => "Sixth"
);
echo "<select id=\"attr_field_10\" name=\"config_eph_payment_type\">";
foreach ($options as $k => $v) {
echo "<option value=\"$k\"";
if ($k == $config_eph_payment_type)
echo " selected";
echo ">$v</option>";
}
echo "</select>";
What we basically do is to store the options into an associative array. Then, we loop through it and keep checking if the value is equal to the variable $config_eph_payment_type.
<option value="5"<?php if ($config_eph_payment_type == 5) print ' selected="selected"'; ?>>First</option>
This is the easiest way to do it. You could also put the if at the beginning of the file and then define a variable, this would make the HTML code more readable
<?php
//at beginning
if ($config_eph_payment_type==5)
$fiveSelected = ' selected="selected"';
else
$fiveSelected = '';
?>
in your code
First
I am trying to modify some code I found on this site (courtesy of mqchen) to enable linking to specific option values. Although it's close to what I'm looking for, I have little php knowledge and therefore need some help. Here's the original code:
<?php
$options = array('Norway', 'United States', 'Springfield');
echo '<select>';
foreach($options as $country) {
if(array_key_exists('selected', $_GET) && $_GET['selected'] === $country) {
echo '<option selected="selected">'.$country.'</option>';
}
else {
echo '<option>'.$country.'</option>';
}
}
echo '</select>';
?>
And the link would be something like:
/countries.html?country=norway
I need to have not only the country between the option tags, but also a value in the option tag, so that the final output would be:
<option value="unitedstates">United States</option>
I was also going to edit the third line to:
echo '<select name="name" size="1" onchange="ShowHide(this.value);">';
because my select tag currently has those attributes for certain functionality. So basically all I need to do, I believe, is add another array containing the option values that line up with the countries and put the variable into the option tag, which I assumed would be:
<option value="'.$valueid.'">
But I'm not exactly sure.
You should do
$options = array('norway' =>'Norway', 'unitedstates' => 'United States', 'springfield' => 'Springfield');
echo '<select name="name" size="1" onchange="ShowHide(this.value);">';
foreach($options as $key => $country) {
if(array_key_exists('selected', $_GET) && $_GET['selected'] === $country) {
echo '<option selected="selected" value="'.$key.' >'.$country.'</option>';
}
else {
echo '<option value="'.$key.'>'.$country.'</option>';
}
}
echo '</select>';
BTW - this is php not jQuery