How to send array by post method - php

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']:""?>"/>

Related

How to use the $_GET function to pull URL data?

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'];

HOW to add increment value to $_POST['variable'] in php?

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.

PHP $_POST is empty when posting hidden fields

I have the following form that is dynamically generated:
<form action="index.php?route=module/print_wizard/showPrintSheet&token=4ef5f4af6ba25d6096357fdb4809e819" method="post" enctype="multipart/form-data" id="form">
<input name="[print][6][1]" type="hidden" value="on">
<input name="[print][6][3]" type="hidden" value="on">
<input name="[info]" type="hidden" value="INV-GIS-00002-3">
<input name="[layout_override][6][1]" type="hidden" value="">
<input name="[layout_override][6][3]" type="hidden" value="">
<input name="[bundle_override][6][1]" type="hidden" value="">
<input name="[bundle_override][6][3]" type="hidden" value="">
<input name="[run_id]" type="hidden" value="14040455">
<button type="submit">Export</button>
</form>
My PHP code is:
var_dump($_POST);
echo "<HR>".$this->request->server['REQUEST_METHOD'];
I have done this a million times before and can not for the life of me figure out why my $_post array is empty. I have changed my post to a get and all the fields and values are passing, but I need to use a post. Do I need to have one visible form element? Please help!
You are not using valid names for your form fields:
<input name="[print][6][1]" type="hidden" value="on">
is not valid as it just has an array index but no name.
If you change it to for example:
<input name="print[6][1]" type="hidden" value="on">
it will work without any problems.

Tomcat 6 & PHP with enctype - No input data returned

I have this problem when I'm using PHP5/HTML on Apache-Tomcat6.
Here's an example for one of the forms I use in my site:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
Whenever I add the 'enctype' attribute to any form; neither the $_FILES['image'] is returned nor the $_POST variables. As long as the 'enctype' is not there, everything (except for the file input of course) works as expected. Can any one guide me please?
You won't be able to post data with a method of get on your form.
In test.html:
<form enctype="multipart/form-data" method="post" action="hello.php" >
<label>Title* :</label>
<input type="text" name="title" />
<label>Image:</label>
<input type="file" name="image" /><br />
<input type="submit" value="Add"/>
</form>
In hello.php:
<?php
print_r($_POST);
print_r($_FILES);
Depending upon your server configuration, this will combine $_GET, $_POST, and $_COOKIE, but you'll still want to post with file inputs.
print_r($_REQUEST);

how to pass values from one page to another on jquery form submit

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.

Categories