php form action path with variable - php

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.

Related

Passing a PHP value to a page and back

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;

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 not executing

I am new to PHP and having trouble with a user form. The code executes and produces a page with a selection box and a submit button. The submit button should prompt a new php file. However, the code in the new PHP file is not being executed. I'm just getting a blank webpage.
<?php
include 'Connection.php';
echo "<form action=\"accountStatusChange.php\" method=\"post\">";
echo "<br/>";
echo "<select name=\"accountStatus\">";
echo "<option value=\"Active\">Active</option>";
echo "<option value=\"Inactive\">Inactive</option>";
echo "</select>";
echo "<input type=\"submit\" name=\"loadAccountStatus\" value=\"Go\"/>";
echo "</form>";
?>
this is file accountStatusChange.php:
<html><body>
<?php
$status = $_POST['accountStatus'];
echo $status;
?>
</body></html>
First of all, make your life easier, try to change your code to:
<?php
include 'Connection.php';
echo '
<form action="accountStatusChange.php" method="post">
<br/>
<select name="accountStatus">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
<input type="submit" name="loadAccountStatus" value="Go"/>
</form>
';
?>
Then in second file:
<html><body>
<?php
if(isset($_POST['accountStatus'])){
$status = $_POST['accountStatus'];
echo $status;
}else{
echo 'form not submitted';
}
?>
</body></html>
Have you uploaded both files to server?
Are both the files in the same directory?
You don't need to use so many echo statements; though there is no problem with your code.
Try with using $_REQUEST; like this:
$status = $_REQUEST['accountStatus'];
<?php
include 'Connection.php';
?>
<form action="accountStatusChange.php" method="post">
<br/>
<select name="accountStatus">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
<input type="submit" name="loadAccountStatus" value="Go"/>
</form>

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