I am pretty new to PHP and have looked for a solution for what I'd have thought would be a common question but I can't find the answer. Any help would be greatly appreciated.
So I have a form with a selector and various options:
<form class="form-horizontal" id="main-home-form" action="trip_search_process.php" method="post">
<div class="form-group">
<label>Select location:</label>
<select class="form-control" id="location_selector" name="location">
<option value="default">I Don't Mind...</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="aus_nz">Australia & New Zealand</option>
<option value="europe">Europe</option>
<option value="north_america">North America</option>
<option value="south_america">South America</option>
</select>
When this form is submitted, I have this PHP code which extracts the location selected:
$location_passed = $_POST["location"];
However, what is passed is the contents of the 'option'. What I would like is the value. I.e. for this 'option':
<option value="aus_nz">Australia & New Zealand</option>
I currently get 'Austraia & New Zealand', whereas I want aus_nz
Is this possible? Or am I going about this the wrong way?
<?php
if(isset($_POST['re'])){
if($_POST['location'] == 'default'){
// error
echo '<p>make a selection</p>';
}else{
$val = $_POST['location'];
echo '<p> selected value; '.$val.'</p>';
}
}
?>
<form class="form-horizontal" id="main-home-form" action="" method="post">
<div class="form-group">
<label>Select location:</label>
<select class="form-control" id="location_selector" name="location">
<option value="default">I Dont Mind...</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="aus_nz">Australia & New Zealand</option>
<option value="europe">Europe</option>
<option value="north_america">North America</option>
<option value="south_america">South America</option>
</select><br>
<input type="submit" name="re" id="re" value="value.">
</div>
</form>
the above code will give the value of the selected option.
Put the php code to your action page trip_search_process.php
<?php
if(isset($_POST['submit'])){
echo $value = $_POST["make"];
echo $text = $_POST["make_text"];
}
?>
<form method=post>
<select name='make' onchange="setTextField(this)">
<option value = '' > Please Select Value </option>
<option value = '5'> Text 5 </option>
<option value = '7'> Text 7 </option>
<option value = '9'> Text 9 </option>
</select>
<br/>
<input type=submit name=submit>
<input id="make_text" type = "hidden" name = "make_text" />
<script type="text/javascript">
function setTextField(ddl) {
document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
}
</script>
</form>
Related
I need help.
I have a select box with items and I want, if the user select a car name and click on "search" I need to have the name of the car he selected on a "echo".
I tried couple of things but it displays nothing (blank page).
below is my code.
Thanks in advance !
<select name="category" >
<option value="nocategory">No Category</option>
<option value="toyota">Toyota</option>
<option value="nissan">Nissan</option>
<option value="mazda">Mazda</option>
<option value="volvo">Volvo</option>
</select>
<input name="submit" value="Search" type="submit">
</form>
<?php
if (isset($_POST['submit'])){
$category = $_POST['category'];
?>
Set form action to $_SERVER['PHP_SELF'] like below: $_SERVER['PHP_SELF'] is a PHP super global variable which returns the filename of the currently executing script.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="category" >
<option value="nocategory">No Category</option>
<option value="toyota">Toyota</option>
<option value="nissan">Nissan</option>
<option value="mazda">Mazda</option>
<option value="volvo">Volvo</option>
</select>
<input name="submit" value="Search" type="submit">
</form>
<?php
if (isset($_POST['submit'])){
$category = $_POST['category'];
echo $category;
}
?>
how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');
I have a form in which values get inserted and passed to PHP, everything was working till I thought about inserting a select value, and this one always gets passed on to the database as 0. This is the form:
<form name="myForm" id="register" action="register.php" method="post" accept-charset="utf-8">
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
This is how I retrieve all my variables, but this select one doesn't seem to work.
PHP:
$value7 = mysql_real_escape_string($_POST['Year']);
Thank you!
Change this
<option value="year1">1</option>
to this
<option value="1">year1</option>
PHP Code
<?php
if(isset($_POST))
echo mysql_real_escape_string($_POST['Year']);
?>
HTML code
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
<input type='submit' >
</form>
change this
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
to
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
I have an php form which contains 7 dropdown lists. When selecting a value from one of this I want the others 6 to return to their default values if they were previously opened. I think it should be used a javascript for this but I don't now to write such a script. I guess each of this dropdown list must have an ID and when I select an option from each one of the 7 the javascript should recognise the id and sets the other to default. I tried using a javascript with document.getElementsByName('id') but it doesn't work.
<form id="form1" name="form1" method="post" action="">
<select name="select" size="1" id="1">
<option value="0" selected="selected">please select</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">red</option>
</select>
<br />
<select name="select2" size="1" id="2">
<option value="0" selected="selected">please select</option>
<option value="1">intel</option>
<option value="2">amd</option>
</select>
<br />
<select name="select3" size="1" id="3">
<option value="0" selected="selected">please select</option>
<option value="1">fish</option>
<option value="2">car</option>
<option value="3">table</option>
</select>
<br />
<select name="select4" size="1" id="4">
<option value="0" selected="selected">please select</option>
<option value="1">lcd</option>
<option value="2">led</option>
</select>
</form>
This is my form.
Let's sey for example I select a value from the second dropdown list "intel". After this i select from the 3rd dropdown list a value "table". What i need to do is when selecting from the 3rd dropdownlist the second one returns to "please select".
The ideea is that using a javascript i do not want those who use the form to select more than one dropdown list at a time.
They should be able to see the values from each one opening the dropdown list but if they open another one the previously must return to the selected="selected" option which is "please select".
Thanks.
Here is a idea to start. Consider 2 dropdown like this.
<select id="Numeric">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="Alphabets">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
Based on the value of 1st dropdown i am selecting the second dropdown
var val = document.getElementById("Numeric").value;
var obj = document.getElementById("Alphabets");
if(val == "1") obj.value = "A"
else if(val == "2") obj.value = "B"
else if(val == "3") obj.value = "C"
Switch is more better
switch(val)
{
case("1"):
obj.value = "A";
break;
case("2"):
obj.value = "B";
break;
case("3"):
obj.value = "C";
break;
}
This is very basic starup idea. You need to implement good logics for your needed functionality
Updated Answer after Comments
<html>
<head>
<script type="text/javascript">
function handleSelect(thisObj){
var list = document.getElementsByTagName("SELECT")
for(var i=0; i< list.length; i++)
{
if(list[i] ==thisObj) continue;
list[i].value = "0";
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<select name="select" size="1" id="1" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">red</option>
</select>
<br />
<select name="select2" size="1" id="2" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">intel</option>
<option value="2">amd</option>
</select>
<br />
<select name="select3" size="1" id="3" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">fish</option>
<option value="2">car</option>
<option value="3">table</option>
</select>
<br />
<select name="select4" size="1" id="4" onchange="handleSelect(this)">
<option value="0" selected="selected">please select</option>
<option value="1">lcd</option>
<option value="2">led</option>
</select>
</form>
</body>
</html>
Can anyone help me edit this page to remember the form values? I think I need to use cookies? Ive searched this question in alot of places but because I have already got scripts in place that change certain aspects of my page I am having trouble working it out.
<html>
<head>
<title>Laterooms App</title>
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width; initial-scale = 1.0; maximum-scale=1.0; user-scalable=no" />
<style type="text/css">
*{margin:2;padding:0}
html, body {width:320;overflow:hidden}
</style>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function submitForm(s) {
s.value = " Loading... ";
return true;
}
// End -->
</script>
<script type="text/javascript">
function changeImg(img, newimg) {
img.src = newimg;
}
</script>
<center>
<form name=myform onSubmit="return submitForm(this.submitbutton)">
<img src="laterooms.jpg"><br><br>
<input type=text value="Destination" name=title onclick="this.value = '';">
<br>Date:<br>
<select name="month">
<option value="1">January
<option value="2">February
<option value="3">March
<option value="4">April
<option value="5">May
<option value="6">June
<option value="7">July
<option value="8">August
<option value="9">September
<option value="10">October
<option value="11">November
<option value="12">December
</select>
<select name="day">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
<option value="13">13
<option value="14">14
<option value="15">15
<option value="16">16
<option value="17">17
<option value="18">18
<option value="19">19
<option value="20">20
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
</select>
<select name="year">
<option value="2012">2012
<option value="2013">2013
<option value="2014">2014
</select>
<br><br>
No. of Nights:
<select name="nights">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
<option value="13">13
<option value="14">14
<option value="15">15
<option value="16">16
<option value="17">17
<option value="18">18
</select>
<input type=submit name=submitbutton value="Submit"><br>
Please be patient<br>while your results load.
<hr color="#401485">
<?php
$url = "http://xmlfeed.laterooms.com/index.aspx?aid=1000&rtype=4&kword=".$_GET['title']."&sdate=".$_GET['year']."-".$_GET['month']."-".$_GET['day']."&nights=".$_GET['nights']."&orderby=hoteldistance&sortorder=asc";
$xml = simplexml_load_file($url);
foreach($xml->hotel as $hotel)
{
echo "<p>";
echo "<img src=".$hotel->images." height=100 width=100><br/>";
echo "<strong>Hotel Name:</strong><br> ".$hotel->hotel_name."<br/>";
echo "<strong>Prices From:</strong> £".$hotel->prices_from."<br/>";
echo "<img src=http://affiliates.laterooms.com/AffiliateImages/en/buttons/more_details1.gif onclick=this.src='loading.gif'><br/>";
echo "<strong>Miles from ".$_GET['title']."</strong> ".$hotel->hotel_distance."<br/>";
echo "</p><hr color=#401485>";
}
?>
</form>
</center>
</body>
</html>
When you submit a form in PHP the $_POST super variable is filled with your form's fields.
As Farhan says above you can just put <?php echo $_POST['fieldname']; ?> into your value property. However that will not help you with your drop down boxes. I would suggest the following:
<input type="text" name="txtName" value="<?php echo (isset($_POST['txtName']) ? $_POST['txtName'] : ''); ?>"/>
The above will do your input fields, the "echo (isset(..." bit will stop PHP from flagging an error if the form has yet to be submitted. The next block will cover your drop down boxes.
<select name="ddlCountry">
<option value="England" <?php echo (isset($_POST['ddlCountry']) && $_POST['ddlCountry'] == 'England' ? ' selected="selected" ' : '');?> >England</option>
<option value="France" <?php echo (isset($_POST['ddlCountry']) && $_POST['ddlCountry'] == 'France' ? ' selected="selected" ' : '');?> >France</option>
</select>
The above block will put a selected flag on your options if the relevant one was answered when the form was posted. I would normally build my drop downs in PHP echoing each option in a loop to cut down on the amount of code required but your way will work just as well.
If you need help understanding the echo ? 'a' : 'b'; method take a look at http://php.net/manual/en/function.echo.php there are some examples half way down the first code block.
You can assign the values by adding the post values to the HTML values like this:
<input type=text value="Destination" name=title onclick="this.value = '';" value="<?php echo $_POST['title'] ?>">
You can set the value="" for each input on your form with the corresponding input name. When the page is posted, the $_POST array contains the values that you just submitted and therefore you can assign it to the proper input tags value.
Hope that helps you to better understand how form memory works in PHP.