Passing a PHP value to a page and back - php

Although sending the form to the receiving page works normally (via $_POST), I'd like to know how it can be sent back through an <a> tag. Is it possible without the use of AJAX or any JS scripts? I'm thinking about using cookies but have no idea on how to set it in-between the PHP/HTML scripts.
Here's a code sample for what I'm doing
Question page:
<form action="results.php" method="POST">
<select name="SampleSelect">
<option>Sample1</option>
<option>Sample2</option>
<option>Sample3</option>
</select>
</form>
Answer page:
Return to questions
<?php
$answer = $_POST['SampleSelect'];
echo $answer;
?>

This could be achieved with sessions
Start or resume a session with session_start() and then store the answer in the session. The code could look like this:
answer.php:
<?php
session_start();
?>
Return to questions
<?php
$answer = $_POST['SampleSelect'];
$_SESSION['answer'] = $answer;
echo $answer;
?>
question.php:
<?php
session_start();
$answer = $_SESSION['answer'];
$options = [
"Sample1",
"Sample2",
"Sample3"
];
?>
<form action="results.php" method="POST">
<select name="SampleSelect">
<?php
foreach ($options as $option) {
if ($option === $answer) {
echo '<option selected>' . $option . "</option>\n";
} else {
echo '<option>' . $answer . "</option>\n";
}
}
?>
</select>
</form>

Yes, you can pass it as a $_GET
$var = 'something';
echo "<a href='questions.php?var=$var'>Pass me back</a>";
In your questions PHP file you'll grab it:
$var = $_GET['var'];
echo $var;

Related

Change value of variable based on Dropdown form selection in PHP

I am currently working with XML files in PHP and would like to be able to load in a specific XML file based on a selection from a dropdown list.
This is code I have been trying so far with help from some other stackoverflow posts about this method:
<html>
<?php
$submittedValue = "";
$value0 = "Route 7602";
$value1 = "Route 7603";
if (isset($_POST["busroutes"])) {
$submittedValue = $_POST["busroutes"];
}
?>
<form action="" name="busroutes" method="post">
<select name="busroutes" onchange="this.form.submit()">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
<?php
if($submittedValue = $value0){
$urlbus = ("https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7602&format=xml");
}
elseif($submittedValue = $value1){
$urlbus = ("https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7603&format=xml");
}
else{
echo "No XML file loaded";
}
$dublinbus_array = simplexml_load_file($urlbus);
echo '<pre>';
print_r($dublinbus_array);
echo '</pre>';
?>
</html>
The first XML file with the stopid of 7602 is displaying correctly but the XML file with stopid of 7603 isn't. I have a feeling im close to something but just can't get over the line.
Any help would be greatly appreciated.
Use "===" for strict comparison not just "=" .
Learn more details here http://www.programmerinterview.com/index.php/php-questions/difference-between-and-in-php/

Is the correct value being shown? PHP

I successfully echoed out the values that I wanted between the tags but value isn't being recognized which I don't understand, I did a test elsewhere and the value is stored.
This is what I am trying to do where $row[1] displays in the drop down but when selected, no value is stored.
echo '<option value="'.$row[1].'">'."$row[1]".'</option>';
alternatively
$val = $row[1];
// or
$val = "$row[1]";
echo '<option value="'.$val.'">'.
$row[1].
'</option>';
This is my test which works
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$something = $_POST['soption'];
$hey = "hey";
}
?>
<html>
<form method="post">
<select name="soption">
<?php
$you = "somevalue";
$some = '<option value="'.$you.'">'.
"something".
'</option>';
echo $some;
?>
<option value="else">real</option>
</select>
<input type="submit" name="submit" value="test">
</form>
<?php echo isset($something)? $something:""; ?>
<?php echo isset($hey)? $hey:""; ?>
</html>
There doesn't seem to be a problem here, I tested your code in my local server and it displayed both $something and $hey in the echo.
The proper syntax for '."$row[1]".' is '.$row[1].' though if your original enclosing quotes under echo were single quotes.

Using html in php

I have a problem with this statement:
after this i just get blank page.
so the whole code looks like this :
im tyrying to use html in php and than php again in html:
the whole rest works and if i replace '' with add2.php it works but it writes something before i even pick something
<?php
function login()
{
echo "?";
}
$get = $_GET['Login'];
$get = $_POST['Login'];
echo $get;
var_dump($get);
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div class="content">
<?php
require 'connection.php';
include 'user_verify.php';
include 'access_verify.php';
mysql_select_db("idoctor_db") or die("Bląd podczas wybierania bazy danych");
$select = 'SELECT * FROM users;';
$query = mysql_query($select);
// Ustaw domyślny element; tutaj są ustawione kreseczki, żeby nic nie sugerować ;P
echo '<form action="'<?php login(); ?>'" method="post">
Jezyk <select name="Login"><option value="0">------------------</option>';
while ($language = mysql_fetch_object($query))
{
echo '<option value="'.$language->Login.'" selected>'.$language->Login.'</option>';
}
echo '</form>';
?>
<input type="submit" value="Login" name="submit"/>
</div>
</body>
</html>
The HTML form element doesn't render anything on screen... add some content and you will see it is working.
<?php
function login()
{
echo "?";
}
?>
<?php
echo '<form action="<?php login(); ?>" method="post">';
echo 'Hello World!';
echo '</form>';
?>
Make sure your quotes are properly closed too. (I'm not sure if the missing closing single quote in your sample was a copy/paste error or not)
<?php
echo '<form action="<?php login(); ?>" method="post">
?>
Is a syntax error, you forgot the closing single quote and semicolon.
That said, I do not think this code will do what you expect: By using echo you will send "<?php login(); ?>" to the browser, not run it in the PHP interpreter.
This:
<?php
echo '<form action="<?php login(); ?>" method="post">
?>
Should be:
<?php
echo "<form action='" . login() . "' method='post'>";
?>
But then your login function needs to be fixed because printing out ? is not going to work.
function login()
{
return "login.php";
}

php form action path with variable

I'm really thinking I must have a badly formed php echo statement at the beginning of the but Dreamweaver is telling me that I have no syntax errors. My process.php is never getting called.
$file = dirname(__FILE__) . '/customBook-index.php';
$plugin_path = plugin_dir_path($file);
$plugin_url = plugin_dir_url($file);
<?php
echo '<form method="post" action="'.$plugin_url.'process.php" />';
echo'<select name="clients">';
foreach($clientsArray as $client){
echo'<option value="'.$client.'">'.$client.'</option>';
}
echo'</select>';
echo '</form>';
?>
You don't have to echo all of your HTML. You could also write:
<?php
$file = dirname(__FILE__) . '/customBook-index.php';
$plugin_path = plugin_dir_path($file);
$plugin_url = plugin_dir_url($file);
?>
<form method="post" action="<?=$plugin_url?>process.php" />
<select name="clients">
<?php
foreach($clientsArray as $client){
?>
<option value="<?=$client?>"><?=$client?></option>
<?php
}
?>
</select>
</form>
Maybe that's easier for you to read and understand!? What is the output of $plugin_url.'process.php in your HTML? I think that either the path does not match or that you don't submit your form correctly.

Why does my php drop down form result in an Undefined offset

Please could you check this code and see why its returning an Undefined offset: and how it can be fixed.
options.php
<?php
$options = array();
$options["PC 1"] = array("year"=>"2000","colour"=>"blue");
$options["PC 2"] = array("year"=>"2003","colour"=>"pink");
$options["PC 3"] = array("year"=>"2006","colour"=>"orange");
?>
index.php
<html>
<body>
<form name="input" action="test.php" method="post">
Device Name: <?php
include("options.php");
echo '<select name="option">';
foreach($options as $name => $values)
{ echo '<option value="' .$name .'">' .$name .'</option>';
}
echo '</select>';
?>
<input type="submit" value="Submit" />
</form>
</body>
</html>
test.php
<?php
include("options.php");
$chosenValue = $_POST['option'];
list($year,$colour) = $options[$chosenValue]; ---- here is the error line
echo $year;
echo $colour;
?>
Thanks
I think it's because the key's in your array $options have spaces in them. Which is completely valid/legal in PHP, but in HTML, when the form is submitted it doesn't like it.
Try changing them to $options["PC1"] and so on and see if it fixes it.
Edit: From the PHP manual - list() only works on numerical arrays and assumes the numerical indices start at 0.
Try changing test.php to:
<?php
include("options.php");
$chosenValue = $_POST['option'];
$year = $options[$chosenValue]['year'];
$colour = $options[$chosenValue]['colour'];
echo $year;
echo $colour;
?>
Change the way you access year and colour in test.php, list() doesn't seem to work well with non-numeric indices. One of the user-contributed comments in the php docs suits your needs
http://www.php.net/manual/en/function.list.php#53420
Or just go for
$data = &$options[$chosenValue];
echo $data['year']; // etc

Categories