Get the Value from another PHP page - php

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">

Related

Insert selected value into get parameter array

I do have a simple html form with select fields. the first field ask for the number of persons. The other fields ask for the age of each person:
<form action="#">
<select name="person">
<option value="0">0 person</option>
<option value="1">1 person</option>
<option value="2">2 persons</option>
<option value="3">3 persons</option>
<option value="4">4 persons</option>
<option value="5">5 persons</option>
</select>
<select name="age_p1">
<option value="0">0 year</option>
<option value="1">1 years</option>
<option value="2">2 years</option>
<option value="3">3 years</option>
<option value="4">4 years</option>
</select>
<select name="age_p2">
<option value="0">0 year</option>
<option value="1">1 years</option>
<option value="2">2 years</option>
<option value="3">3 years</option>
<option value="4">4 years</option>
</select>
<input type="hidden" name="personandage" value="[X,X,X]" >
<input type="submit">
</form>
So if I have e.g. 3 persons, 3 new select field appears to insert the age of each person.
An example:
person 1 = 1 years; person 2 = 4 years; person 3 = 5 years
After submitting the form GET value have to be in an array like this personandage=[1,4,5]
Does someone has an idea how to realize this with PHP? Thank you!
Try this on the html:
<form action="#">
<select name="person">
<option value="0">0 person</option>
<option value="1">1 person</option>
<option value="2">2 persons</option>
<option value="3">3 persons</option>
<option value="4">4 persons</option>
<option value="5">5 persons</option>
</select>
<select name="age[]">
<option value="0">0 year</option>
<option value="1">1 years</option>
<option value="2">2 years</option>
<option value="3">3 years</option>
<option value="4">4 years</option>
</select>
<select name="age[]">
<option value="0">0 year</option>
<option value="1">1 years</option>
<option value="2">2 years</option>
<option value="3">3 years</option>
<option value="4">4 years</option>
</select>
<input type="hidden" name="personandage" value="[X,X,X]" >
<input type="submit">
</form>
and then you can $_GET['age'] and should returns an array with the ages.
I would do it this way, by making the age_p1, age_p2 .... into an array called lets say ages[] This way it does not matter to the PHP how many are used in the HTML or whether you add them dynamically with javascript.
<?php
$pna = '[';
if ( isset($_GET['submit'] ) {
$pna = $_GET['person'];
foreach ( $_GET['ages'] as $age ) {
$pna .= ",$age";
}
}
$pna .= ']';
?>
<form action="#">
<select name="person">
<option value="0">0 person</option>
<option value="1">1 person</option>
<option value="2">2 persons</option>
<option value="3">3 persons</option>
<option value="4">4 persons</option>
<option value="5">5 persons</option>
</select>
<select name="ages[]">
<option value="0">0 year</option>
<option value="1">1 years</option>
<option value="2">2 years</option>
<option value="3">3 years</option>
<option value="4">4 years</option>
</select>
<select name="ages[]">
<option value="0">0 year</option>
<option value="1">1 years</option>
<option value="2">2 years</option>
<option value="3">3 years</option>
<option value="4">4 years</option>
</select>
<input type="hidden" name="personandage" value="<?php echo $pna;?>" >
<input type="submit" name="submit">
<!-- ^^^^^^^^^^^^^ -->
<!-- note I added an name attribute -->
</form>
Like the others said, put square brackets after the select. So
<select name="age[]">
...options...
</select>
<select name="age[]">
...options...
</select>
Would then be refered to as
_$GET["age"][0]
And
_$GET["age"][1]
Edit:
Sorry, noticed you wanted the array as personandage.
One option is to set the array on the page that your form redirects you to:
$array_index = $_GET["person"] - 1
for ($i = 0, $i <= $array_index, $i++) {
${"personandage" . $i = array("p1", _$GET["age"][$i]);
}
Now your arrays should look like this
$personandage1
$personandage2
Etc...
Hope I understood your question :)
//written from My cellphone, havent tested the code.

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>

Upload images PHP to MySQL and retrieve correct images to each form

I have to re-explain my situation they said.
my html form:
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="carform.css"/>
<head>
</head>
<body>
<form action="connection.php" method="post" class="smart-green">
<h1> Car listing
<span>Please fill all the text in the fields.</span>
</h1>
<label>
<span>Make:</span>
<select id="category">
<option value="Alfa Romeo">Alfa Romeo</option>
<option value="Aston Martin">Aston Martin</option>
<option value="Audi">Audi</option>
<option value="Austin">Austin</option>
<option value="Bentley">Bentley</option>
<option value="BMW">BMW</option>
<option value="Buddy">Buddy</option>
<option value="Buick">Buick</option>
<option value="Cadillac">Cadillac</option>
<option value="Chevrolet">Chevrolet</option>
<option value="Chrysler">Chrysler</option>
<option value="Citroen">Citroen</option>
<option value="Dacia">Dacia</option>
<option value="Daewoo">Daewoo</option>
<option value="Daihatsu">Daihatsu</option>
<option value="Dodge">Dodge</option>
<option value="Ferrari">Ferrari</option>
<option value="Fiat">Fiat</option>
<option value="Fisker">Fisker</option>
<option value="Ford">Ford</option>
<option value="GMC">GMC</option>
<option value="Honda">Honda</option>
<option value="Hummer">Hummer</option>
<option value="Hyundai">Hyundai</option>
<option value="Infiniti">Infiniti</option>
<option value="Isuzu">Isuzu</option>
<option value="Iveco">Iveco</option>
<option value="Jaguar">Jaguar</option>
<option value="Jeep">Jeep</option>
<option value="Kewet">Kewet</option>
<option value="Kia">Kia</option>
<option value="Koenigsegg">Koenigsegg</option>
<option value="Lada">Lada</option>
<option value="Lamborghini">Lamborghini</option>
<option value="Lancia">Lancia</option>
<option value="Land Rover">Land Rover</option>
<option value="Lexus">Lexus</option>
<option value="Lincoln">Lincoln</option>
<option value="Lotus">Lotus</option>
<option value="Maserati">Maserati</option>
<option value="Maybach">Maybach</option>
<option value="Mazda">Mazda</option>
<option value="McLaren">McLaren</option>
<option value="Mercedes-Benz">Mercedes-Benz</option>
<option value="Mercury">Mercury</option>
<option value="MG">MG</option>
<option value="MINI">MINI</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Morgan">Morgan</option>
<option value="Morris">Morris</option>
<option value="Nissan">Nissan</option>
<option value="Oldsmobile">Oldsmobile</option>
<option value="Opel">Opel</option>
<option value="Peugeot">Peugeot</option>
<option value="Piaggio">Piaggio</option>
<option value="Plymouth">Plymouth</option>
<option value="Pontiac">Pontiac</option>
<option value="Porsche">Porsche</option>
<option value="Renault">Renault</option>
<option value="Reva">Reva</option>
<option value="Rolls Royce">Rolls Royce</option>
<option value="Rover">Rover</option>
<option value="Saab">Saab</option>
<option value="Seat">Seat</option>
<option value="Skoda">Skoda</option>
<option value="Smart">Smart</option>
<option value="Ssangyong">Ssangyong</option>
<option value="Subaru">Subaru</option>
<option value="Suzuki">Suzuki</option>
<option value="Tazzari">Tazzari</option>
<option value="Tesla">Tesla</option>
<option value="Think">Think</option>
<option value="Toyota">Toyota</option>
<option value="Triumph">Triumph</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Volvo">Volvo</option>
<option value="Others">Others</option>
</select>
</label>
<label>
<span>Title:</span>
<input id="title" type="text" name="title">
</label>
<label>
<span>Price:</span>
<input id="price" type="text" name="price">$
</label> <label>
<span>Description:</span>
<textarea id="description" name="description" rows="20" cols="60"></textarea>
</label>
<label>
<span> </span>
<input type="button" class="button" value="Send"/>
</label>
</form>
</body>
</html>
and my php:
<?php
$con = mysqli_connect("", "", "", "");
if (mysqli_connect_errno()) {
echo "Failed to connect to mysqli: ".mysqli_connect_error();
}
$cat = mysqli_real_escape_string($con, $_POST['category']);
$tit = mysqli_real_escape_string($con, $_POST['title']);
$pri = mysqli_real_escape_string($con, $_POST['price']);
$des = mysqli_real_escape_string($con, $_POST['description']);
$sql = "INSERT INTO forsale (Category, Title, Price, Description) VALUES ('$cat', '$tit', '$pri', '$des')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
echo "1 record added";
mysqli_close($con);
?>
Now what I am missing is having uploading maximum of 12 images for this form per Id and being able to retrieve all the images that belongs to eash posting
your code if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
you are missing a closing curly brace like this, not sure if this is your onle problem but it certainly is a problem.
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));}

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

Remember form field values

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.

Categories