Pass a PHP Variable into HTML Select "Value" - php

I am trying to pass some php via a dropdown in html , the php then needs to be used to aid the execution of more php. However I do not think it is working. Any suggestions would really be appreciated, its been bugging me a while. This is not the first form to use post on this page, the dropdown is populated after a search has been performed using another previous form.
HTML:
<form method="post" name="results">
<select class="form-control textinput">
<option value="<?php $response->body->results[0]?>"><?php echo $response->body->results[0]->name; ?></option>
<option value="<?php $response->body->results[1]?>"><?php echo $response->body->results[1]->name; ?></option>
<option value="<?php $response->body->results[2]?>"><?php echo $response->body->results[2]->name; ?></option>
<option value="<?php $response->body->results[3]?>"><?php echo $response->body->results[3]->name; ?></option>
<option value="<?php $response->body->results[4]?>"><?php echo $response->body->results[4]->name; ?></option>
</select>
<button type="submit" class="btn btn-primary" style="margin-left: auto; margin-right: auto; text-align: center;">Go</button>
</form>
PHP:
<?php
//Results from dropdown are put into selection
$selection = $_POST["results"];
//Selection is put into result var, should look like $response->body->results[x]
$result = $selection;
// IF Statement to only print result if the api call is successfull
if ($response->code == 200) {
if ($result->name == null) {
$printthis = "{$gametitle} returned no results, try and enter the full and accurate name";
}
else {
$printthis = "{$result->name} has a score of {$result->score} on {$result->platform}";
}
}
?>

First, you missed the 'echo' statement, and you also can't put an entire object into a value. If
$response->body->results[0]
has an id, thats what you should be using
something like this
<?php echo $response->body->results[0]->id ?>
I would do this with a loop.
for($i = 0; $i < count($response->body); $i++) {
echo '<option value="'.$i.'"><'.$response->body->results[$i]->name.'</option>';
}

Related

How to pass two id values from one drop down with PHP

I have one dropdown with a foreach loop which will pass to model using post method.
<div class="form-group" ">
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php
foreach ($relationship as $row) {
?>
<option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
<?php
}
?>
</select>
</div>
and in the model it is getting the proper values using post method. As
$rel = $_POST['rel'];
Here the problem is when the user select one option ,I want to get two values like
"<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"
like
$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID
I want to get both from one selection without adding any visble dropdown or element..
Try bellow code with ajax call
In Javascript code get value
var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php foreach ($relationship as $row) { ?>
<option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
<?php } ?>
</select>
</div>

Codeigniter, problems using set_select() and set_checked()

I have a bit of an issue using codeigniter's set_select and set_checked within my forms, I am adding these to my existing forms as I am the stage in development where I am trying to tidy things up and failed validation resetting forms was not a big issue when I was still working on the project but now its coming to a close its become a major headache.
Firstly the set_select, I have this code which outputs me drop down from an array which is passed to the view from the controller which gets the results from a table in my database, the form I am implementing this in has 10 drop down boxes each corresponds to a table in my database. Anyway this is the code:
<label for="rating">Rating: </label>
<select name="rating">
<?php
if(isset($rating) && $rating != 'none') {
echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
foreach($rating as $row) {
echo '<option value="'.$row->door_rating_rating.set_select('rating', $row->door_rating_rating).'">'.$row->door_rating_rating.'</option>';
}
} else {
echo '<option value="none">Nothing to list</option>';
}
?>
</select>
It is just not working and as far as I can see there shouldn't be a problem with my code but this is the first time I have used this and I have looked at examples of using it but could not find an example of using it in a for loop so is what I am doing even possible?
This is my set_checked code within the view and this too is not working after failed validation:
Temporary Refuge Door?<input type="checkbox" class="temp_ref" name="tempref" value="1" <?php echo set_checkbox('tempref', '1'); ?> />
Any help with either of these would be really appreciated.
Looks like you had the set_select within the quotes for the option value. I moved it after it. I have also made an edit to use printf for better readability.
<label for="rating">Rating: </label>
<select name="rating">
<?php
if(isset($rating) && $rating != 'none') {
echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
foreach($rating as $row) {
printf('<option value="%s" %s>%s</option>', $row->door_rating_rating, set_select('rating', $row->door_rating_rating), $row->door_rating_rating);
}
} else {
echo '<option value="none">Nothing to list</option>';
}
?>
</select>
To solve set_select() try the following. Assuming you have an array named $isps which contains id and name field.
<select id="isp" name="isp">
<option value="" selected>Select a ISP</option>
<?php foreach ($isps as $row) { ?>
<option value="<?php echo $row->id ; ?>" <?php echo set_select('isp', $row->id, False); ?> ><?php echo $row->name ; ?> </option>
<?php } ?>
</select>

how to get selected username from dropdown list to be the var for URL

How do i get the selected variable to show up in the URL from the
<select> ...
my code is:
print "<select name=\"assignedby\" multiple size=\"10\">";
while ($data = dbResult($qh)) {
print "<option name=\"$data[name]\"";
print ">$data[name]</option>\n";
}
print "</select>";
print "<br><a href='".$_SERVER['PHP_SELF']."?action=add'>Add</a> || <a href='".$_SERVER['PHP_SELF']."?origname=$data[name]'>Edit</a>\n";
When someone clicks on "EDIT" link its showing up as: http://www.site.com?origname=
i want it to show up with the actual selected origname from the drop down list...
like:
http://www.site.com?origname=$selecteduser-fromdroplist
please help!
Why don't you just make the form method = get?
<form id="select_name" action="" method="get">
<select name="origname">
<?php while ($data = dbResult($qh)): ?>
<option value="<?php echo $data[name]; ?>"><?php echo $data[name]; ?></option>
<?php endwhile; ?>
</select>
Add || <input type="submit" name="submit" value="Edit">
</form>
But as for why your code doesn't work, you're calling the $data[name'] item outside the while loop.

Get option of a select when the option choice changes

I Have a select list, each option contains a category ($k[3]) in its id.
<select name="page_from_link" class="my_select_list" >
<OPTION VALUE="" >With:</OPTION>
<?php foreach($pages_created as $k) {
$i=0; ?>
<OPTION id="<?php $i."-".$k[3]; ?>" class="pages_created" VALUE="<?php echo $k[0]; ?>" ><?php echo $k[1]; ?></OPTION>
<?php } ?>
</select>
I have another select list with all options hidden depending on the precedent select category :
AS:
$v) { ?>
" >
my jquery script doesn't work, how would you have done that ?
$(document).ready(function(){
$(".my_select_list").change(function(){
var array = $(this).find('option:selected').attr('id').split('-');
var cat = array[1];
alert("cat");
$("#cat_"+cat).show();
$(".pages_created_option:not(#cat_"+cat).hide();
return false;
});
});
$(this > option).attr(... => $(this).find('option:selected').attr (...
alert("cat"); => alert(cat);
$("#cat_"+cat+")").show(); => $("#cat_"+cat).show();
you should consider using firebug or chrome internal development tools (on F12).
on a side note, you might like to use optgroup instead of a dummy option:
<select name="page_from_link" class="my_select_list" >
<OPTGROUP label="With:">
<?php foreach($pages_created as $k) {
$i=0; ?>
<OPTION id="<?php $i."-".$k[3]; ?>" class="pages_created" VALUE="<?php echo $k[0]; ?>" ><?php echo $k[1]; ?></OPTION>
<?php } ?>
</OPTGROUP>
</select>
you could use either a php script, or a js script with the data encoded, holding the innerHTML and that changes the innerHTML of the parent object (such as a div) depending on what you selected in the previous select box.
add this in the head for javascript
function showsel()
{if(document.getElementById('parentid').value=="id of the option that will triger the shild ")
{document.getElementById('shild id').style.display="inline";}
else {document.getElementById('shild id').style.display="none";}
}
Then add for the shild
div id="shild id" style="display:none"
and to the parent
div id="parent id" onchange"showsel()"

How to Keep the selected value of the select box after Form POST or GET

Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>

Categories