Insert $_POST into HTML form action - php

I want to insert $_POST value into the HTML form action. By using my code I got this error message: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) ....
This is my sample code in my PHP file:
print <<< HERE
<form action="../../path/to/file.php?id=<?php echo $_POST['mytext'];?>" method="POST">
// some stuff here ...
<input type="text" name="mytext" id="mytext" value="">
<input type="submit" value="Submit">
</form>
HERE;
What is wrong with my code?

You don't need to add id in the form action
<form action="../../path/to/file.php" method="POST">
// some stuff here ...
<input type="text" name="mytext" id="mytext" value="">
<input type="submit" value="Submit">
</form>
Instead in the file.php you can make a variable and assign $_POST['mytext'] to that variable
$id = $_POST['mytext'];

Related

syntax error, unexpected '<' using eval and base64

I'm trying to encode a simple code but I'm getting this error:
Parse error: syntax error, unexpected '<' in C:\wamp\www\test.php(2) : eval()'d code on line 1
This is the code:
<form method="POST" action="">
Enter your command: <input type='text' name='cmd'> <input type='submit' name='execute' value='Execute'>
</form>
echo $_POST['cmd'];
This is the base64_encode with eval:
<?
eval(base64_decode('PGZvcm0gbWV0aG9kPSJQT1NUIiBhY3Rpb249IiI+CkVudGVyIHlvdXIgY29tbWFuZDogPGlu cHV0IHR5cGU9J3RleHQnIG5hbWU9J2NtZCc+IDxpbnB1dCB0eXBlPSdzdWJtaXQnIG5hbWU9J2V4ZWN1dGUnIHZhbHVl PSdFeGVjdXRlJz4KPC9mb3JtPgoKCmVjaG8gJF9QT1NUWydjbWQnXTsK'));
?>
That is because you are trying to eval non-php code (html form)
You forgot the PHP tags around your code:
<?php
// Any PHP code as needed
?>
<form method="POST" action="">
Enter your command: <input type='text' name='cmd'> <input type='submit' name='execute' value='Execute'>
</form>
<?php
echo $_POST['cmd'];
?>

How to get the value of a textbox on the same page in PHP

Here is my code:
<form action="" method="get" >
<input type="text" name="un">
<input type="password" name="un2" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
$users1 = $_GET["un"];
$id = $_GET["un2"];
echo $users1;
?>
I am unable to display it through this way
error:
Notice: Undefined index: un in C:\wamp\www\vas1\register1.php on line 31
line 31:
$users1 = $_GET["un"];
It's just a notice. You need to check if the form is being submitted:
if(!empty($_POST)) {
$users1 = $_POST['un'];
echo $users1;
}
You can't be using using get because your form is using post:
<form action="" method="post">
You have a couple issues in your code.
Firstly, on the first page load the textarea hasn't been submitted so the request data will be blank. You'll need a isset() to test for it.
Secondly, your PHP is using $_GET when your form is using POST so you need to change those.
Putting it all together:
<form action="" method="post" >
<input type="text" name="un">
<input type="password" name="un2" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
if (isset($_POST['un'])) {
$users1 = $_POST["un"];
$id = $_POST["un2"];
echo $users1;
}
?>
you are sending the post request
<form action="" method="post" >
and then you are getting the parameter
in get request
$users1 = $_GET["un"];
you are doing wrong ..do this
$users = $_POST["un"];

passing data using php and html

I am trying to pass a certain data which I found in my table using the search to another php page . here is my code
echo "
1<form action="adm_edit.php?product_code=$record[0]" method="POST">
2<input type=submit value=Edit>
3</form>
4<form action="adm_edit.php?product_code=$record[0]" method="POST">
5<input type=submit value=Delete>
6</form>
";
my search function is working fine and record[0] is contain desired data but I am getting this error when I run this code:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in search.php on line 1
I put numbers on lines in above code for ease of reading
So could you please help me?
Thank you
Take care when using quotes from html elements in echos, also when using variables!
When using ' instead of ", you also have to put quotes in front of the variable and that way you stop echoing a string and can start with echoing a variable. You need to concatenate the var and the string with a . !
This will work :
echo '
<form action="adm_edit.php?product_code='.$record[0].'" method="POST">
<input type=submit value=Edit>
</form>
<form action="adm_edit.php?product_code='.$record[0].'" method="POST">
<input type=submit value=Delete>
</form>
';
<form action="adm_edit.php?product_code=<?php echo $record[0]; ?>" method="POST">
<input type=submit value=Edit>
</form>
<form action="adm_edit.php?product_code=<?php echo $record[0]; ?>" method="POST">
<input type=submit value=Delete>
</form>

How to convert a HTML Form Variable Passed to a PHP Script as a String?

So I have this HTML:
<form action="tickeroutput.php">Ticker Symbol: <input type="text" name="ticker" method="get"/>
<input type="submit" value="Submit" />
</form>
And then I want to use the data contained in ticker, however I have this in tickeroutput.php, and it doesn't seem to want to work:
$ticker = $_GET("ticker");
Is this not the correct format to have $ticker as a string? I specifically need what the user inputs as a string variable.
First of all I think you meant to write :
<form action="tickeroutput.php" method="get">Ticker Symbol:
<input type="text" name="ticker"/>
<input type="submit" value="Submit" />
</form>
and to fix your problem you should replace $ticker = $_GET("ticker");
with $ticker = $_GET["ticker"]; replace () with []
You need to use square brackets, not (). $ticker = $_GET['ticker'] $_GET is an array.

How can I solve a PHP syntax error that I receive when parsing html code?

Trying to print out some html forms but I get a parsing syntax error. I believe it gets stuck on the SERVER[PHP_SELF] but I'm not sure. How can I get this to echo correctly?
Error occurs on the SERVER[PHP_SELF] line
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
function drawbuttons()
{
echo <<<EOT
<table>
<tr><td>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="submit" name="previous" value="Previous" STYLE="font-size:12pt; background-color:00BFFF; color:ffffff">";
</form>
</td>
<td>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="submit" name="next" value="Next"STYLE="font-size:12pt; background-color:00BFFF; color:ffffff">
</form>
</td></tr>
</table>
EOT;
}
From the manual on the heredoc syntax:
Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.
That means you can’t use PHP open or close tags and you need to use the proper syntax for variables. In this case use the curly brace syntax:
echo <<<EOT
<table>
<tr><td>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="submit" name="previous" value="Previous" STYLE="font-size:12pt; background-color:00BFFF; color:ffffff">";
</form>
</td>
<td>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="submit" name="next" value="Next"STYLE="font-size:12pt; background-color:00BFFF; color:ffffff">
</form>
</td></tr>
</table>
EOT;
You can't use PHP opening tags (short or not) inside heredocs.
Use instead:
<form action="{$_SERVER['PHP_SELF']}" method="post">

Categories