Pre fill a input field from incoming link
We have a Quenstion and Answer script we have made.
Essentially, the form element looks like this ( I think the issue I have is POST and not GET )
Anyway will ask anyway.
On the Q and A page, users can enter title and question. So form looks a tad like this:
<form method="post">
<input id="askit" type="text" name="q_desc" value="">
...blah blah blah
</form>
Basically, I want to add a input field on an external page ( same domain ) so that when user clicks submit, it takes them to the Q and A page, and propagates the input field in the form above with the value of what they typed on previous page.
For this purpose I have added the id="askit"
So basically, the e开发者_运维知识库xternal page would fire something like this:
www.somewebsite.com/questions/?askit=hello+world
Because my form uses Post, I am pretty sure this is where it is going pear shaped.
Any suggestions please. ? The form must be POST because it is for submitting questions.
To get http://www.somewebsite.com/questions/?askit=hello+world
to working you need to update your code above in the following way:
<?php
$askit = '';
if(array_key_exists('askit', $_GET)) {
$askit = $_GET['askit'];
}
?>
<form method="post">
<input id="askit" type="text" name="q_desc"
value="<?php echo htmlentities($askit); ?>">
...blah blah blah
</form>
You can still mix and match POST and GET data on this form using the $_POST
and $_GET
PHP super globals, respectively.
<form method="post">
<imput id="askit" type="text" name="q_desc" value="<?php echo isset($_GET['askit']) ? htmlspecialchars($_GET['askit']) : ''; ?>">
</form>
Although I am not recommending it, you're life might be easier by using $_REQUEST
which is a merge of both POST and GET data.
its really easy, all you need to do it get the value submitted by the user using $_GET['']
or $_POST['']
depending on the request, and then echo it as a value of the input, so something like :
<input id="askit" type="text" name="q_desc" value="<?php echo $_GET['askit']?>">
you can always use htmlentities() to make sure that the form is safe and prevent people messing with your html.
精彩评论