echo php variable string cut off after ampersand - php

I submit the s variable to page.php from an input form on a different page.
The URL has the complete string with the ampersand, when I try to echo the variable, the string is cut off after the ampersand.
form:
<form action="page.php" method="get">
<input type="text" name="s" id ="s" />
</form>
Variable is:
search & search
URL:
page.php?s=search+%26+search&var2=variable
variable echoes as:
search
I have tried:
echo htmlspecialchars($_GET['s']);
echo htmlentities($_GET['s']);
echo urldecode($_GET['s']);

You could use (at least) 3 differents ways to archieve this:
1 - urlencode(): use this function in the place you should generate the url to encode the string for URL format.
2 - use %26 (without the "+");
3 - Use $_SERVER['QUERY_STRING']: if you have only one param, you could get all the query_string this way.
UPDATE:
I.E:
$url = "page.php?s=".urlencode('search & search')."&var2=".urlencode('variable'); //or urlencode($variable)
Then use: urldecode() to parse the differents $_GETs.

I assume you have a code similar to this,
<a href="http://localhost/example/page.php?s=search+%26+search">
This kind, will result only the search after you try to echo it.
Try this way,
<a href="http://localhost/example/page.php?s=<?php echo urlencode("search+%26+search") ?>">
it's a convenient way to encode a query part of the url.
and also try this one, an example from the official php documentation
<?php $query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
and for more information, please refer to this.
http://php.net/manual/en/function.urlencode.php

I just tried what you said and the problem doesn't seem to be the encoding of url.
Try this out https://www.google.co.in/?q=search+%26+search , Google searches for search & search, there might be a problem on page.php where you are trying to echo the variable.
Additionally, to prove that google is not doing anything special i just made two files named index.php and page.php and the result was as expected.
index.php
<html>
<body>
<form action="1.php" method="get">
<input type="text" name="s" id="s">
</form>
</body>
</html>
page.php
<?php
echo $_GET['s'];
?>
output:
search & search
Try to debug your code on page.php where you are trying to echo the variable or paste the whole code here.

Try this though I test it locally, here's the code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Problem</title>
</head>
<body>
<form action="page.php" method="get">
<input type="text" name="s" id ="s" />
<input type="text" name="var2" id ="d" />
<input type="submit" name="name" value="submit">
</form>
</body>
</html>
and page.php
<?php
if (isset($_GET['s'])) {
echo $_GET['s'] .' '. $_GET['var2'];
}
?>
when you run it to your browser, it will display two input form and a submit button.
The output will be
search & search variable
I try also pasting in the url
page.php?s=search+%26+search&var2=variable&name=submit
I result the same
I hope this would help

Related

Why is global scope $_GET not seen?

One of my pages (video.php) is opened using form action as follows:
<?php
//Lots of code, including a WHILE loop
echo "<form action=\"video.php?id=".$row['id']."\" method=\"post\" target=\"_top\">
<input type=\"image\" src=\"".$image."\" style=\"width:180px;height:120px\"
alt=\"Submit\"></form>";
?>
On the page video.php?id, I get the id as follows and declare other global scope vars. However, why is the $_GET variable not seen in my echoed alert when I submit a form as in the following simplified code?
//video.php?id page
<?php session_start();
include 'connect.php';
$Vid = mysqli_real_escape_string($_GET['id']);
$login_id = mysqli_real_escape_string($_SESSION['login_id']);
if (isset($_POST['sample'])) {
echo "<script>
alert('$Vid');
</script>";
}
else//etc.
?>
<html><head></head><body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" id="Form">
<button name="button" type="submit">Click</button>
<input type="hidden" name="hidden" value="sample">
</form>
</body></html>
When I alert $Vid, nothing is alerted (blank alert box). Obviously, I see the SESSION variable when I alert $login_id. Am I missing something with the $_GET? Is there any way for the global var $Vid to be recognized? If I could use $Vid it would save me 5 or 6 queries based on how my code is currently written.
This how you can correct
Put $row['id'] inside a hidden text box with name as "id"
Your form method is POST, so use $_POST to grab the data in the POST file.
I think you escaped wrongly and thus the id is not appended (notice the backslash after $row['id']?), try the following:
echo '<form action="video.php?id='.$row['id'] . '" method="get" target="_top">
<input type="image" src="' . $image . '" style="width:180px;height:120px"
alt="Submit"></form>";
Imho your coding style is very unreadable with all those backslashes. It's okay to mix single/double quotes where needed…
[edit] and you obviously need to change the method to "get". ;-)
Changed action from $_SERVER['PHP_SELF'] to action="" and now the GET variables are seen.

How to retain search query after clicking search and printing result on same page using PHP

I have created my PHP page where I have search query field. After submitting query I am printing result on same page. The query is working fine but I want query to be displayed even after result displayed. i.e. query is being disappeared after result comes. How can I retain the query word/s along with result in webpage. This might be very basic and sounds like stupid but since I am newbie and tried so many ways but in vain.
Below is my code:
<html>
<head>
<TITLE>PHP FORMS</TITLE>
</head>
<body algin=center>
<p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter Drug Name <input type="text" name="drugName"></p>
<p><input type="submit" value="search"></p>
</form>
</body>
</html>
Can anyone suggest valuable idea and cause for it?
try this
<input type="text" name="drugName"
value="<?php echo (isset($_POST['drugName']) ? $_POST['drugName'] : '') ?>">
Change your form method to GET and append as many strings as you want. Then via PHP, use $_GET['varstring'] value.
OR if you must use POST
foreach ($_POST as $set => $myval){
echo "{$set} = {$myval}\n";
}

PHP $_POST doesn't display data - Example code shown

I am new to PHP, I tried to work w3 schools example of posting data on forms..
It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!
Example #1: A simple contact from - HTML code
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2: Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Expected output of this script may be:
Hi Joe. You are 22 years old.
Actual Output:
Hi . You are years old
The Post parameter is not displaying data.. Any help is really appreciated.
What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.
However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)
If you wish to make use of htmlspecialchars, do the following in your welcome.php file:
<?php
$fname = htmlspecialchars($fname);
?>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.
Form used:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname );
And placed within <?php and ?> tags such as:
<?php
$fname = htmlspecialchars( $fname );
?>
NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO
mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.
Please visit this answer
StackOverflow did not let me insert the codes on that page, for one reason or another.
In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?
//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
.
//action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT 2
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
'post' or 'POST' both works fine in form tag.
The following should be in action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT
and check whats the error.
Most probably it should work now...
In your form check if it is not sending empty values.
STEP 1
copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.
The following code should be in index.php
<form action="action.php" method="POST" onsubmit="return validate()">
<p>Your name: <input type="text" name="name" id="name"/></p>
<p>Your age: <input type="text" name="age" id="age"/></p>
<p><input type="submit" /></p>
</form>
<script type="text/javascript">
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
alert ("Name="+name+" age="+age);
return false;
}
</script>
This code will check if the values are getting entered correctly without redirecting the page to action.php.
Step 2
If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
if (name==null || name==""){
return false;
}
if (age==null || age==""){
return false;
}
return true;
}
If both name and age are filled in the form, the submit will now redirect to action.php
Step 3
In action.php use the following code.
<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>
edited with instructions on OP's request
Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding

Losing my $_GET variable upon form submission

I'm creating a small application which allows potential employees to list references. The listed references receive an email containing a URL with a unique string at the end.
(Example: www.the-address.com?url=503241c65b8fe4_07914393). The reference then follows this unique URL to upload a letter in the employee's behalf.
But every time any form is submitted, the random string part of the URL disappears
(Example: www.the-address.com?url=).
I don't understand why this would happen, since I submit the form like this:
<form action="upload_letter.php?url="' . $url . '" id="form_id" method="POST">;
Where $url = $_GET['url'].
Any generic reasons this would happen? I can provide more code, if needed.
If you really have the code like you write, you're closing the action attribute prematurely with the second " character. Try this instead:
echo '<form action="upload_letter.php?url='.urlencode($url).'" id="form_id" method="POST">';
The way you have it would end up as HTML like:
<form action="upload_letter.php?url="google.de" id="form_id"...>
With google.de outside the attribute value.
<?php
$data = array('url' => $url);
?>
<form action="upload_letter.php?<?php echo http_build_query($data) ?>" id="form_id" method="POST">
Or you can just add the URL as a hidden <input>
<form action="upload_letter.php" id="form_id" method="POST">
<input type="hidden" name="url" value="<?php echo htmlentities($url); ?>">
.
.
.
</form>
Then you can access URL via $_POST['url'].
Change method="POST" to method="GET"
If your code is what you wrote on your PHP file: it is wrong. No ";" at the end of an HTML line, and you can't concatenate strings with "." in HTML. You must open the PHP tag and write PHP code inside. For example:
<form action="upload_letter.php?url=<?php echo $url; ?>" id="form_id" method="POST">
But you can also use echo like Wolfgang answer
Probably $url is empty or undefined.
Check the HTML code to see if its written into the form's action.
why you put single quotes around:
. $url .
?
EDIT: Another way to say this:
Are you sure you're on a <?php ?> tag?

Write text into input field with a PHP variable

I would like to write in an input field with a PHP variable. This is the current code I have:
JS:
<script type="text/javascript">
function reply(form, inp){
form.texta.value = "#" + inp;
}
</script>
HTML:
Reply ^
<form id="form1" name="form1" method="post" action="index.php"><input type="texta" name="texta" /></form>
This code however does not put the text of the variable in the input field. Sorry, I am a bit new to JavaScript.
Can anyone help me here?
Using my code I gave in the question and due to the answers/comments i have put single quotes around the php, and now it works perfectly. Thanks!
<input type="text" name="texta" value="<?php echo $poster; ?>" />
Reply ^
EDIT : You should Give Id to text box.
if you want it direct
<input type="texta" name="texta" value="<?=$poster?>" />
Or
use document.form.texta.value = value
or like this
document.forms["form"]["texta"].value = value
put any of these inside
function reply(form, inp){
}
Reply ^

Categories