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.
Related
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;
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/
<?php
$tab=$_GET['liste'];
$max=sizeof($tab);
echo$max;
echo$_GET['type'];
if ($_GET['type']='multiple')
{
?>
<form name='form'>
<select size=<?php $max ?> multiple>
<?php
for($i=0;$i<$max;$i++){
echo"<option value=$i>$tab[$i]</option>";
}
echo"</select>";
echo"</form>";
}
else{
?>
<form name='form'>
<select size=<?php $max ?>>
<?php
for($i=0;$i<$max;$i++){
echo"<option value=$i>$tab[$i]</option>";
}
echo"</select>";
echo"</form>";
}
?>
in two cases it's showing just the dropdown list width single choice even if $_GET['type']='multiple' .
I tried
<select size=<?php $max ?> <?php echo$_GET['type']; ?>>
instead of
<select size=<?php $max ?> multiple>
But still not working !!
Any suggestions please !
Change
<select name='ville' size="$max" multiple>
to
<select name='ville' size="<?=$max?>" multiple>
There is a lot that needs fixing here (no offense), so let's start with
if($_GET['type']='multiple')
All this is doing is assigning the value 'multiple' the $_GET['type'] variable, so this will always hit. Next, as mentioned before, you need to change
<?php $max ?>
as it currently does nothing. Next up is
echo"<option value=$i>$tab[$i]</option>";
When using an array element in a string, must be wrapped with { and }
Let's take a look at some cleaned up code because there are too many errors here to pinpoint the real issue:
<?php
$tab = $_GET['liste'];
$max = sizeof($tab);
#echo $max;
#echo $_GET['type'];
$multiple = '';
if($_GET['type'] == 'multiple')
{
$multiple = ' multiple="multiple"';
}
?>
<form name='form'>
<select size="<?php echo $max ?>" <?php echo $multiple; ?>>
<?php
foreach($tab as $key => $value)
echo "<option value='{$key}'>{$value}</option>";
?>
</select>
</form>
Edit: Just verified - variables inside a string must always be wrapped in { and } if it's referencing an array element (this does not apply to objects after all):
$a = array('foo' => 'bar');
$o = new stdclass;
$o->foo = 'bar';
$o->baz = array('foo' => 'bar');
echo "$a['foo']"; // Syntax error
echo "$o->foo"; // echos "bar"
echo "{$a['foo']}"; // echos "bar"
echo "$o->baz['foo']"; // Syntax error
In general, it's a Good Idea to always wrap variables in strings with { and }, even if it's legally allowed:
echo "Something {$here}"; // Obvious variable is obvious
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.
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