Input value based on query string - php

How could I pass a query string value such as domain.com/register?invite=MR5OMxTyjYmTjcwNTyQjTZMyY5YY into a input box on a page?
For example say I had this: <input type="text" name="invite" value="" />
I'm using PHP
To clarify what I mean, if a person loaded that URL, then the value would be automatically filled in with the query string of invite.

Simple:
<?php $invite = (array_key_exists('invite', $_GET)) ? htmlspecialchars($_GET['invite']) : ''; ?>
<input type="text" name="invite" value="<?php echo $invite; ?>" />

Try this:
<input type="text" name="invite" value='<?php echo $_GET["invite"]; ?>' />

<input type="text" name="invite" value="<?php if(isset($_GET['register'])) echo $_GET['register']; ?>" />
This isn't secure at all, but it gives you a start.

Try fetching the invite key with $_GET['invite'] from the address bar (validate it first of course to prevent XSS attacks ;) ) and then place it in your input field within that value part as $invite for example so you end up with value="$invite"
Hope that helps!

<input type="text" name="invite" value="<?php echo $_REQUEST['invite']; ?>" />

Related

search form prevent remove other query

html:
<form method="GET">
<input type="text" name="k" id="header-search" value="<?=$_GET["k"];?>"/>
<input type="submit" id="header-submit" value="" />
</form>
When current url is:
https://example.com/search/cats?b=5
After click on submit button it remove b query and show like this:
https://example.com/search/cats?k=sometext
But i want this result:
https://example.com/search/cats?b=5&k=sometext
I have other query like b, d and also c maybe add more in future, so this is not a static, maybe url have b maybe d or maybe c or maybe all together or maybe no one.
I tried this but looks like no changes:
action="<?=$_SERVER['REQUEST_URI'];?>"
You can add the variables inside hidden inputs:
<form method="GET">
<?php if(isset($_GET['b']){ ?>
<input type="hidden" name="b" value="<?=$_GET["b"];?>"/>
<?php } ?>
<input type="text" name="k" id="header-search" value="<?php echo isset($_GET["k"]) ? $_GET["k"] : '';?>"/>
<input type="submit" id="header-submit" value="" />
</form>
However this solution will not work very well if you have many different types of variables that may or may not exist all the time. If you add all the variables as hidden, they will all be visible when you submit the form. To prevent this, you will need to check if the variables are isset() and only print them if they are.
Here is a solution that uses hidden fields and handles any amount of get parameters:
<form method="GET">
<?php
foreach($_GET as $key => $value){
// do not make a hidden input for k, there is already a text input for k
if($key != 'k'){
echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
}
}
?>
<input type="text" name="k" id="header-search" value="<?php echo isset($_GET["k"]) ? $_GET["k"] : '';?>"/>
<input type="submit" id="header-submit" value="" />
</form>

form submit only giving last value in a loop codeigniter

Here is my view
<?php for($i=0;$i<count($acb['def']);$i++) { ?>
<input type="text" name="xyz" value="<?php echo $abc['def'][$i]?>" />?>
Here is my controller
$xxx=$this->input->post('xyz')
Now when i submit the form the last value only gets posted to controller
then i found that the name is same for all fields so it takes last value , so i changed the input name as
name ='xyz[$i]'
Now i need to post values , How to post values with this
You need to send name as array rename it to 'xyz[]' here
<input type="text" name="xyz[]" value="<?php echo $abc['def'][$i]?>" />?>
You use to below code...
<?php for($i=0;$i<count($acb['def']);$i++) { ?>
<input type="text" name="xyz[]" value="<?php echo $abc['def'][$i]?>" />
?>

Codeigniter set_value() foreach loop

I generate a form, which mostly consists of input fields that are already populated with values from the db.
So I do this currently like so:
<input id="misc" name="misc" value="<?php echo $workout['misc']; ?>" />
But when I try and do this:
<input id="misc" name="misc" value="<?php echo set_value($workout['misc']); ?>" />
along with a validation rule, the form does reload itself, the error message does display BUT the form is reset
What am I doing wrong?
As per the manual:
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The above form will show "0" when loaded for the first time.
Hence in your case:
<input id="misc" name="misc" value="<?php echo set_value('misc', $workout['misc']); ?>" />
OR
<input id="misc" name="misc" value="<?php echo set_value('misc'); ?>" />
Documentation:
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

How do I receive post from a form where I name an input as <?php echo $var ?>?

I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />

How can I set the value of a textbox through PHP?

So I have this empty textboxes in a registrationg page. The user enters some data, hits continue and then there's a confirmation page. If the data is incorrect, the user hits go back to go correct whatever was wrong. However, when he goes back, all the textboxes are empty. So the first thing that comes to my mind is to store the user data in a Session (I have a User class that holds all this data so I store the class in the session). When the user goes back I am able to retrieve the data.
I do something like this:
if($_SESSION['UserInfo'])
{
$user = $_SESSION['UserInfo'];
$firstName = $user->FirstName;
$lastName = $user->LastName;
}
How would I put these variables in a textbox?
To set the value, you can just echo out the content inside the value attribute:
<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />
Of course you will want to escape it but...
<input type="text" value="<?php echo $firstName ?>" />
or if the form is posted, it would be easier to do:
<input type="text" name="firstName" value="<?php echo $_POST['firstName'] ?>" />
fine... even though it was out of the scope of the question here is the escaped version:
<input type="text" name="firstName" value="<?php echo htmlentities($_POST['firstName']) ?>" />
smth like
<input type="text" value="<?php echo $first_name;?>">
Don't forget to escape with htmlentities() or smth similar. If you don't know why - google XSS.

Categories