I am trying to understand how to use the $_GET function. I am using a form from my CRM system Infusionsoft. I have a few different ads running to this form from different sources. I use UTM's to keep track of where people are coming from.
Example URL UTM: http://www.awesome.com/?utm_source=google&utm_medium=cpc&utm_term=keywords&utm_content=content&utm_campaign=name
The hidden field was my attempt to try to pull the data and pass it along when the form is submitted. Unfortunately it is not working and I am a bit of a newbie to php.
<form accept-charset="UTF-8" action="https://mk165.infusionsoft.com/app/form/process/ff023ed2f8ffd7c46b03cdc50a115e93" class="infusion-form" method="POST" name="myForm" onsubmit="return validateForm()">
<input name="inf_form_xid" type="hidden" value="ff023ed2f8ffd7c46b03cdc50a115e93" />
<input name="inf_form_name" type="hidden" value="Ninh Neuropathy" />
<input name="infusionsoft_version" type="hidden" value="1.48.0.46" />
<input type="hidden" id="inf_custom_GaSource" name="inf_custom_GaSource" value='<?php echo $_GET['utm_source']?>'/>
<input type="hidden" id="inf_custom_GaMedium" name="inf_custom_GaMedium" value='<?php echo $_GET['utm_medium']?>'/>
<input type="hidden" id="inf_custom_GaTerm" name="inf_custom_GaTerm" value='<?php echo $_GET['utm_term']?>'/>
<input type="hidden" id="inf_custom_GaCampaign" name="inf_custom_GaCampaign" value='<?php echo $_GET['utm_campaign']?>'/>
<input type="hidden" id="inf_custom_GaContent" name="inf_custom_GaContent" value='<?php echo $_GET['utm_content']?>'/>
<input class="infusion-field-input-container" placeholder="First Name" name="inf_field_FirstName" type="text" />
<input class="infusion-field-input-container" placeholder="Last Name" name="inf_field_LastName" type="text" />
<input class="infusion-field-input-container" placeholder="Phone" name="inf_field_Phone1" type="text" />
<input class="infusion-field-input-container" placeholder="E-mail" name="inf_field_Email" type="text" />
<input class="infusion-field-input-container submit" id="submit" name="" value="Get In Touch Today" type="submit" />
</form>
Your form method is POST so when you submit the form you will get the values in php as:
<?php
print_r($_POST);
?>
You will get the all form input values in $_POST Super Global included all hidden inputs.
One last thing also use double quotes in hidden input values instead of single quotes as:
<input type="hidden" id="inf_custom_GaSource" name="inf_custom_GaSource" value="<?php echo $_GET['utm_source'];?>"/>
Often both types of parameters are needed. It might make things easier if GET and POST parameters are normalized and then can be accessed in a unique way like this:
$params = array();
foreach ($_GET as $k=> $v) $params [$k] = $v;
foreach ($_POST as $k=> $v) $params [$k] = $v;
echo $params['myvalue'];
Related
Let's say I have a form that I send via GET:
<form method="get" action="/search.php?foo=bar&test=1&something=else">
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
And after submitting my form and processing the data (which consists of only saving it to a file), the url is changed to:
search.php?day=test&link=google.com
What should I do so the url becomes:
/search.php?foo=bar&test=1&something=else&day=test&link=google.com
(preserving the old parameters that were included in action attribute.)
The form action will change every time and it's difficult to keep the old GET parameters in the form action.
However, you can go with hidden fields.
Try this:
<form method="get" action="/search.php">
<input type="hidden" name="foo" value="bar"/> <!-- Add this -->
<input type="hidden" name="test" value="1"/> <!-- Add this -->
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
You could try changing the action before submitting, depending how you're going to submit the form. This can be done if you apply IDs to your texts and either a name or an ID to the form.
HTML:
<form id="frm" method="get" action="/search.php?foo=bar&test=1&something=else">
<input type="text" name="day" id="day" placeholder="day"/>
<input type="text" name="link" id="link" placeholder="link"/>
</form>
Then with JavaScript you can run a function and change the form's action:
var _form = document.getElementById('frm');
var day = document.getElementById('day').value;
var link = document.getElementById('link').value;
_form.action += '&day=' + day + '&link=' + link;
_form.submit();
You could try to use a hidden input field.
<input type="hidden" name="name" value="value">
if you are getting your variables first from get and then want to add into 2nd form you could get this and create input fields hidden with these get values
Now when you submit form in get url you will get all you desire data
<?php
if($_GET){
if(isset($_GET['submit1'])){
$foo = $_GET['foo'];
$test = $_GET['test'];
$something = $_GET['something'];
?>
<form method="get" action="/search.php">
<input type="hidden" name="foo" placeholder="foo" value="<?php echo $foo; ?>"/>
<input type="hidden" name="test" placeholder="test" value="<?php echo $test; ?>"/>
<input type="hidden" name="something" placeholder="something" value="<?php echo $something; ?>"/>
<input type="text" name="day" placeholder="day"/>
<input type="text" name="link" placeholder="link"/>
</form>
<?php
}
}
?>
i have a problem keeping a post element in variables before calling one more time post in a second form .
briefly:
variable below contain elements from previous post
$img=isset($_POST['image'])?$_POST['image']:false;
$table=isset($_POST['tabl'])?$_POST['tabl']:false;
Then i have as next to the lines above the form below :
<form method="post" action="" style="padding-top:200px;padding-bottom:200px;padding-left:500px">
<div><span>Email</span></div> <input type="text" value="" name="user"/><br><br>
<div><span>Password</span></div> <input type="password" value="" name="pass"/><br><br>
<input type="submit" value="send" name="submit"/>
</form>
and right after pressing submit $img and $table are both empty .
how can i keep the values in $imgand $table even after calling post again ?
any clue ?
Thanks
You aren't sending the variables you want in the POST data in your second form.
You could use a hidden field to send it,
A bit like this.
<form method="post" action="" style="padding-top:200px;padding-bottom:200px;padding-left:500px">
<input type="hidden" name="img" value="<?php echo $img;?>" />
<input type="hidden" name="table" value="<?php echo $table;?>" />
<div><span>Email</span></div> <input type="text" value="" name="user"/><br><br>
<div><span>Password</span></div> <input type="password" value="" name="pass"/><br><br>
<input type="submit" value="send" name="submit"/>
</form>
I am using dynamic form where user add more input text boxes for a certain field he want and the name of each box change with an increment like:
<form method="post" action="somescript.php">
<input type="text" name="textbox" />
<input type="text" name="textbox1" />
<input type="text" name="textbox2" />
<input type="text" name="textbox3" />
.... and so on
</form>
I want to echo these data following a loop:
<?PHP
$k=$_POST['counter']; //counter value coming as post variable
for($i=1$i<=$k;$k++){
echo $_POST['textbox'.$i]; //something like this......?
}
?>
Please reply.
Use array notation instead.
<form method="post" action="somescript.php">
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox[]" />
<input type="text" name="textbox][" />
.... and so on
</form>
When the form is submitted, $_POST['textbox'] will then be an array, and you can loop over it:
foreach ($_POST['textbox'] as $textbox) {
echo $textbox;
}
I just came across this issue because I had blocks of data that needed to be created dynamically and
echo $_POST["textbox$i"];
worked without the concatenation in it. Let me know if this is bad practice, it works in my situation though. The array way didn't work for me. Sorry for posting this on a 3 year old question. I'm not sure if that's bad practice. Thanks.
Following is my sample form.
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="submit" NAME="submit" VALUE="Submit" />
</form>
Basically I have 10 inputs of array. Assume my domain is http://domain.com and the file above is index.php. I am trying to fill the form automatically by using the following method.
http://domain.com/index.php?array[]=John&array[]=Kelly ... & array[]=Steven
Unfortunately, it is not working. :(
Have you tried something like this:
<?php
foreach( $_GET['array'] as $arr ) // Loop through the `array` variables of GET
echo '<input type="text" name="array[]" value="' . $arr . '" />'; // Display the the inputs
?>
However, please make sure that you use a cleaning function on $arr to prevent XSS. You will also need to check if $_GET['array'] is set or not, or PHP will whine about it.
Two things to try.
You are using the GET method with your example URL, but your form is set to POST. In your PHP, you may be looking for your data in $_POST when it's actually in $_GET. You can get data from both POST and GET using the $_REQUEST variable.
You may need to urlencode the []. ([] becomes %5B%5D when urlencoded.)
Lastly, a tip: when crafting links to send data through a GET query string, it is best practice to use & in place of the &'s in the URL.
If you are trying to fill the form from a GET request (your URL string), you have to get the values from the HTTP request in the $_REQUEST array (or $_GET or $_POST depending on the form method). Suggestion: you should change the names of the form fields to reflect the value they are storing
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="first_name" value="<?php echo $_REQUEST['first_name']?>"/>
Note: accessing the $_REQUEST array as shown above is not good practice as you need to check if the request variables are set before you can echo their values.
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="first_name" value="<?php echo isset($_REQUEST['first_name'])?$_REQUEST['first_name']:""?>"/>
I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.