I'm trying to echo $url in the hidden field but I can't get it to work. I'm already inside an echo and I've tried escaping but that just gives me a parse error.
Sorry if this is a bit vague, Please can somebody help me to echo $url in that value???
echo'<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="echo $url;" readonly/>
</form>';
Thanks
1) Don't echo so much using PHP, you are literally torturing it
2) You are using ' so whatever you write between will be treated as a literal string
3) If it's so, you need to concatenate the string using a period .
4) Use double quotes " when you are using variables inside a string
?>
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="<?php echo $url; ?>" readonly/>
</form>
<?php
echo'
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="'.$url.'" readonly/>
</form>
';
This is the correct usage.
Try like this
<input type="hidden" name="current_url" value="'.$url.'" readonly/>
or simple do like this
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="<?php echo $url;?>" readonly/>
</form>
Don't echo inside echo
echo'
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="', $url, '" readonly/>
</form>
';
Just off topic: Why commas instead of . for concatenation.
You can't echo an echo. You're already echoing out the string so all you need to do is add your variable to it:
change
value="echo $url;"
to
value="' . $url . '"
You could simply use double quotes and let the variable expand in the string, just so:
$url = 'http://www.example.com';
echo "Hello World! This is my url $url";
Related
When I click submit, the data is submitted to the database but the URL id of the book disappears to ----book.php
I want the URL to go back to the id of the page e.g. ----book.php?id=3
Is it possible to keep the first line as action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" and add the value="<?php echo $book_id ?>"?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="book_id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly /></p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
PHP code:
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
}
Please try this code:
// save values in DB
header('Location: book.php?id=' . $book_id);
exit;
It is because $_SERVER["PHP_SELF"] contains only URL . It does not contain get queries . To solve the problem leave your action empty
e.g
<form method="post" action="">.....
You send a POST form but are trying to retrieve a GET value. Additinally, the parameter is named book_id, not id.
Use $book_id = $_POST['book_id'];
if (isset($_GET['id'])) { also won't work, for the same reasons.
There is no name with "id"
If you want to get the book id:
if (isset($_POST['book_id'])) {
$book_id = $_POST['book_id'];
}
OR modify the HTML like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly />
</p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
On the other hand, if you have problems with POST, just modify the first line of HTML for this one:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . echo $book_id; ?>">
You can use anyone of these:
PHP_SELF returns just URL. Use REQUEST_URI that returns URL with query string:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
[you can also omit action values - that will have same behavior]
or if you want just id, then use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . $_GET['id'];?>">
Note: Use validation wherever necessary. I just gave you an idea.
looks like ordinary question but could not find correct solution. lets say I have this form:
<form name="form" action="nextpage.php">
<input type="text" name="input_name" value="<?php echo $_POST['input_name'];?>" />
<input type="submit" name="submit" value="Next"/>
</form>
For example if I add "My input text >" as value of the input the nextpage.php is broken.
I tried to use for:value="<?php echo strip_tags($_POST['input_name']);?>" or
:value="<?php echo htmlspecialchars($_POST['input_name']);?>" but none of them works.. Why is that and how to avoid it? Thanks
First, you should sanitize your posted value for security, but that aside, php provides a function that automatically escapes special characters called addslahes http://php.net/manual/en/function.addslashes.php, save your post value to a variable using the addslashes function, then use the variable as your value.
try this
<?php
$_POST['input_name']="ddd"; ?>
<form name="form" method="POST" action="nextpage.php">
<input type="text" name="input_name" value="<?php echo $_POST['input_name'];?>" />
<input type="submit" name="submit" value="Next"/>
</form>
and in nextpage.php
<?php
$name=$_POST['input_name'];
echo $name;
Try this:
<form name="form" action="nextpage.php" method="POST">
<input type="text" name="input_name" value="<?php echo $_REQUEST['input_name'];?>" />
<input type="submit" value="Next"/>
</form>
I have tried to put this code in my webpage but I have this error :
Parse error: syntax error, unexpected '?' in E:\MyServer\htdocs\...(line 1 from this script)
CODE :
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
this code is in a .php page
Thanks.
Try to format it properly:
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
How to get the value you pass through your form:
Welcome <?php echo $_POST["name"]; ?><br>.
EDIT
Also you should add this in delete.php file:
if(isset($_POST['name'])){ $name = $_POST['name']; }
See : http://www.w3schools.com/php/php_forms.asp
You need to drop the double quotes around the variable name and move them to the actual element property.
<form action='delete.php?name="<?php echo $contact['name']; ?>"'
^ ^
It then becomes
<form action="delete.php?name=<?php echo $contact['name']; ?>"
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
This should work ..
Do you need to send the data via GET and POST to delete.php?
<form action='delete.php' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
With this you can then just do $_POST['name'] to get your contact name...
Sorry if I have missed something but cannot see why you would need it passed twice
<form method="POST" name="send"
<input type="hidden" name="title" value="<?php echo ($pro->title;?)">
</form>
I dont want people to see the hidden informations , Is this the best way to pass the variable over to my controller?
This the code in my controller
$this->email->subject('subject '.$_POST['title'].' ' );
Thank you!
<form method="POST" name="send"
<input type="hidden" name="title" value="<?php echo ($pro->title;?)">
should be
<form method="POST" name="send">
<input type="hidden" name="title" value="<?php echo $pro->title; ?>" />
For this i will suggest you to use SESSION because hidden fields can be changed.
I'm trying to echo a value in a form based on number of rows returned from database query. Keep getting error Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'
As you can probably tell I'm pretty new to this. Can anyone help me echo the variable? I know that $num_rows is returning a value as using var_dump shows. Thanks
<?
if($num_rows <= 10) {
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
}
if($num_rows >10) {
echo '</br></br><form id="h2" class="rounded" action="4.php"
target="_blank" method="post"/>
<input type="submit" name="submit" class="button" value="11"/><BR>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
}?>
In both of your code blocks, you repeat the command echo instead of either concatenating the output or using two statements. You have done this:
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
which is a syntax error. Instead, you can do this:
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="' . $num_rows . '"/>
</form>';
or this:
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="';
echo $num_rows . '"/>';
echo '</form>';
This is the code you should use to concatenate strings and output the result
echo ' some value ' . $variable . ' other text ';
The echo function outputs a string, while the dot (.) operator concatenates strings. This is the kind of wrong code
echo 'value="'echo $num_rows;'"/>';
When you want to insert the value of a variable this is the way
$a_string = 'I\'m a string';
echo "I'm a double quoted string and can contain a variable: $a_string";
This works with arrays too
$an_array = array('one', 'two', 'three');
echo "The first element of the array is {$an_array[0]}"
See the PHP manual