Remember form field values - php

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.

Related

How to echo selected value of dropdown in php?

I want to echo selected value from dropdown. But it always give me the output: Default
Kindly check what I am doing:
<div>
<select id="orderStatus" name="orderStatus">
<option value="0"></option>
<option value="Available">Available</option>
<option value="Arranging Product">Arranging Product</option>
<option value="Awaiting Fulfillment">Awaiting Fulfillment</option>
<option value="Awaiting Payment">Awaiting Payment</option>
<option value="Awaiting Pickup">Awaiting Pickup</option>
<option value="Awaiting Shipment">Awaiting Shipment</option>
</select>
// I think here is the problem when I click button it forgets selected index value
Update
</div>
Php Code:
<?php
$draft = isset($_GET['orderStatus'])? $_GET['orderStatus'] : 'DEFAULT';
echo $draft;
?>
When I click on update button then in the output I always get DEFAULT. I think when I click on the update button it forgets the selected index value.
Kindly suggest what I am doing wrong.
Add to tag onmousedown event:
<div>
<select id="orderStatus" name="orderStatus">
<option value="0"></option>
<option value="Available">Available</option>
<option value="Arranging Product">Arranging Product</option>
<option value="Awaiting Fulfillment">Awaiting Fulfillment</option>
<option value="Awaiting Payment">Awaiting Payment</option>
<option value="Awaiting Pickup">Awaiting Pickup</option>
<option value="Awaiting Shipment">Awaiting Shipment</option>
</select>
// I think here is the problem when I click button it forgets selected index value
Update
</div>
This send your parametr as GET request
<div>
<form method="GET" action="order.php">
<select id="orderStatus" name="orderStatus">
<option value="0"></option>
<option value="Available">Available</option>
<option value="Arranging Product">Arranging Product</option>
<option value="Awaiting Fulfillment">Awaiting Fulfillment</option>
<option value="Awaiting Payment">Awaiting Payment</option>
<option value="Awaiting Pickup">Awaiting Pickup</option>
<option value="Awaiting Shipment">Awaiting Shipment</option>
</select>
<button type="submit" class="btn btn-large">Update</button>
</form>
</div>
Try This , Its working :
<?php
if(isset($_POST['submit'])){
$value = $_POST['orderStatus'];
echo $value;
}
?>
<form action="" method="post">
<select id="orderStatus" name="orderStatus">
<option value="0"></option>
<option value="Available">Available</option>
<option value="Arranging Product">Arranging Product</option>
<option value="Awaiting Fulfillment">Awaiting Fulfillment</option>
<option value="Awaiting Payment">Awaiting Payment</option>
<option value="Awaiting Pickup">Awaiting Pickup</option>
<option value="Awaiting Shipment">Awaiting Shipment</option>
</select>
<input type="submit" name="submit" value="Update" />
</form>

how to check if a dropdown list, multiple=multiple, is selected in php?

I have a multiple=multiple drop down list and I want to check if a user has selected any option or not using PHP code. If not, then I want to display an error message. How can I do that?
<select name="ddlskill" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
You should declare your html like this :
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
And then in PHP, you'll get an array of values :
if( !empty($_POST['ddlskill']) ){
print_r($_POST['ddlskill']);
// [PHP, JSP, Java, ...]
}
Maybe helpful,
<?php
if( isset($_POST) && !empty($_POST['ddlskill'])){
echo "<pre>";print_r($_POST['ddlskill']);
// [PHP, JSP, Java, ...]
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
<button type="submit"> Submit </button>
</form>
<?php
if(!empty($_POST['submit'])){
if(is_array($_POST['ddlskill']))
{
foreach($_POST['ddlskill'] as $key=>$value)
{
print_r($value);
}
}
else
{
print_r($_POST['ddlskill']);
}
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
<option value="Cake PHP">Cake PHP</option>
<option value="Database">Database</option>
<option value="hello">hello</option>
<option value="Java">Java</option>
<option value="Jquery">Jquery</option>
<option value="JSON">JSON</option>
<option value="JSP">JSP</option>
<option value="Mysql">Mysql</option>
<option value="PHP">PHP</option>
<option value="xyz">xyz</option>
<option value="XYZ2">XYZ2</option>
</select>
<button type="submit" name="submit"> Submit </button>
</form>

Get the Value from another PHP page

Hello I have the below form in my page viewsensordata.php
...
$refresh = ($_POST['refresh']);
$link_address="viewsensordata.php?view=".$view;
?>
<form method="post" action="<?php echo $link_address;?>">
<select id="refresh" name="refresh">
<option value="-">Select Seconds</option>
<option value="1">1 Second</option>
<option value="2">2 Seconds</option>
<option value="3">3 Seconds</option>
<option value="4">4 Seconds</option>
<option value="5">5 Seconds</option>
<option value="10">10 Seconds</option>
<option value="15">15 Seconds</option>
<option value="30">30 Seconds</option>
<option value="60">60 Seconds</option>
</select>
<input type="submit" value="Refresh" name="refresh_rate"/>
</form>
<meta http-equiv="Refresh" content="<?php echo $refresh;?>;URL=refreshsensorData.php">
<br />
And I want to get the value of the $refresh to another page named viewsensordata2.php
In my viewsensordata2.php i have $refresh = ($_GET['refresh']); in order to GET it from the other page but it doesnt work. How can I fix that?
<?php
$link_address="viewsensordata.php?view=".$view;
?>
<form method="post" action="<?php echo $link_address;?>">
<select id="refresh" name="refresh">
<option value="-">Select Seconds</option>
<option value="1">1 Second</option>
<option value="2">2 Seconds</option>
<option value="3">3 Seconds</option>
<option value="4">4 Seconds</option>
<option value="5">5 Seconds</option>
<option value="10">10 Seconds</option>
<option value="15">15 Seconds</option>
<option value="30">30 Seconds</option>
<option value="60">60 Seconds</option>
</select>
<input type="submit" value="Refresh" name="refresh_rate"/>
</form>
Then in your second page, you can refresh it like this:
<?php
if(isset($_POST) && isset($_POST['refresh'])){
$sec = $_POST['refresh'];
}
elseif(isset($_GET) && isset($_GET['seconds'])){
$sec = $_GET['seconds'];
}
else{
$sec = 10; // your defualt seconds, if it is not set from form or URL
}
?>
<meta http-equiv="Refresh" content="<?php echo $sec ;?> URL=<?php echo $_SERVER['PHP_SELF']; ?>?seconds=$sec">

I have several dropdown list - I want to print out selected options of these dropdown lists

This is what I got so far, it's just the dropdown list. It's a form for making a reservation for appointment. I want to print out these selected values (= selected options of dropdown)
But I have no clue since I'm very new with PHP and HTML. Something with $_POST and $_GET ? Could I have some suggestions of solution? Much gratitude.
<html>
<head>
<title>title</title>
</head>
<body>
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</body>
</html>
You're missing the <form> tag. It should be wrapped around all your form-data (select, input) etc. Then you will fetch it using either $_GET or $_POST or even $_REQUIRE
Example (change site.php to whatever you please):
<html>
<head>
<title>title</title>
</head>
<body>
<form method="post" action="site.php">
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</form>
</body>
</html>
PHP example:
if(isset($_POST['Ar'])) { // Make sure it's sent
$year = $_POST['Ar'];
echo "År: " . htmlentities($year); // Output (htmlentities for safety when outputting)
$month = $_POST['Manad'];
echo "Månad: " . htmlentities($month);
}

when selecting a value from a dropdown list set other dropdown list to it's default value

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>

Categories