This question already has answers here:
Generate Array from a comma-separated list - PHP [duplicate]
(3 answers)
Closed 8 years ago.
I have a form that asks for comma separated phone numbers, for example, if user enters following in "Phone" field:
9999999999,8888800000,7777788888
Then want to store them as an array just like:
$contacts = array ("9999999999","8888800000","7777788888");
How can I do that?
I tried:
$contacts = array();
if (is_array(#$_POST['phone']))
{
foreach($_POST['phone'] as $one)
{
$contacts[] = basename($one);
}
}
$myArray = explode(',', '9999999999,8888800000,7777788888');
You will have to fetch the form input with the global variable called $_POST, $_POST listens to the name of the html element.
Let's say we got
<form method="POST">
<input type="text" name="phoneNumber1" />
<input type="text" name="phoneNumber2" />
<input type="text" name="phoneNumber3" />
</form>
Then we are able to fetch the data in it like this.
$contacts = array($_POST['phoneNumber1'], $_POST['phoneNumber2'], $_POST['phoneNumber3']);
Related
This question already has answers here:
PHP How to send multiple values within the same name
(2 answers)
I am trying to insert multiple input value with same name
(2 answers)
How to get multiple input values with PHP when name attributes are the same?
(1 answer)
store multiple value with same input name in sql
(1 answer)
PHP Get multiple values from GET with same name
(2 answers)
Closed 4 months ago.
simple php code that works well in terminal:
<?php
$a = array();
for($i=0; $i<3; $i++){
$b = readline('time: ');
$c = readline('money: ');
$d = array('time'=>$b, 'money'=>$c);
array_push($a, $d);
}
print_r($a);
this pushes the values of multiple entries into an array, creating an array of arrays. however, readline() does not work in the browser. i know i can use javascript easily enough but am trying to replicate this simple action using only php and html. and i really like the way readline() works. i have tried variations on the following but am left scratching my head:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time'>
<input name = 'money'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
was hoping print_r($_POST['input name']) would return an array, but instead only gives the last input entry. is there a straight forward way to do this with php, or do i HAVE to use a client-side script like javascript?
Just add [] to input names:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time[]'>
<input name = 'money[]'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
This question already has answers here:
How to get PHP $_GET array?
(8 answers)
Closed 2 years ago.
I got a challenge in which I have this code in a PHP file:
<?php
include('secretfile.php');
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if (!empty($a) && !empty($b))
{
if($a===$b)
{
if(isset($_GET['a']) && isset($_GET['b']))
{
$a = $_GET['a'];
$b = $_GET['b'];
if($a!==$b)
{
echo $secretcode;
}
}
}
}
}
I have to print secretcode in webpage with OUT changing the PHP file.
How can I do it?
I tried by giving parameters through URL like this:
http://127.0.0.1:8080/?a=1&b=1&a=22&b=33
But it didn't work. The file is taking the last values directly and no matter what, I couldn't go past the 13th line. I went through a lot of answers but I go no solution.
Is it possible to do it? If yes, how?
To pass multiple values for a or b, you need to use the [] notation like below:
http://127.0.0.1:8080/?a[]=1&b[]=1&a[]=22&b[]=33
That being said, the PHP code in your file seems to be poorly written.
To pass an array on html form you can use [] like
<form method="GET">
<input type="text" name="a[]" />
<input type="text" name="b[]" />
<form/>
This question already has answers here:
Get $_POST from multiple checkboxes
(5 answers)
Getting checkbox values on submit
(10 answers)
Closed 3 years ago.
Currently i have a HTML form that collects some metadata about some files, and each user as to fill some fields. I want to register some keywords about the data. I could ask them to write the keywords by hand on a normal text box, but i would prefer to have a list of checkboxes of about 10/15 values.
Then i need to pass only the checked values to a PHP file, using $_POST. My problem is that i assign a variable to those values, and then i call that variable in a DOM event. I´m generating a XML file, and currently i have the HTML ready to register these keywords in text input. I understand how to create the checkboxes, and pass them as an array to PHP., by reading other questions here. But i can´t understand how to pass this array to a $dom->createElement situation, and preferably separated by commas.
PHP
//Pull data from HTML form
$keywordsString = $_POST['keywords'];
// Creates xml document
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$xmlFileName = 'example_example.xml';
// Adds metadata to xml
$metadata = $dom->createElement('MD_Metadata');
$idInfo = $dom->createElement('identificationInfo');
$descriptiveKeywords = $dom->createElement('descriptiveKeywords');
$CharacterString = $dom->createElement('CharacterString', $keywordsString);
$descriptiveKeywords->appendChild($CharacterString);
$idInfo->appendChild($descriptiveKeywords);
$metadata->appendChild($idInfo);
$dom->appendChild($metadata);
$dom->save($xmlFileName);
I can´t figure out how to pass checkbox values to that $keywordsString , but separated by commas. The rest i can understand and write, using the other questions about this kind of issue.
Thanks in advance for all help provided.
Your html should look like this:
<form action="" method="post">
<input name="choice[]" type="checkbox" value="1" />
<input name="choice[]" type="checkbox" value="2" />
<input name="choice[]" type="checkbox" value="3" />
<input name="choice[]" type="checkbox" value="4" />
<input type="submit" value="order" />
</form>
In PHP, you can iterate over using foreach loop. Place the below code at the top of your html form:
A small note from basics, when posting data via post method, you need to apply Post redirect get design pattern to prevent form re-submissions.
If the data you are sending is not sensitive, you can do it via get request.
<?php
if($_POST):
foreach($_POST['choice'] as $val)
{
echo $val . "<br>";
}
endif;
?>
This question already has answers here:
foreach checkbox POST in php
(2 answers)
Closed 7 years ago.
Im trying to gather checkbox informtion in a php form. Everythig else is working perfectly, but when selecting 5/10 checkboxes it only gives me the last one checked. When using "services[]" array, it just emails back "services:array"
Any ideas would be great.
Thanks.
(this is the PHP im using)
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$services = $_POST['services'];
$message = $_POST['message'];
$changes = $_POST['changes'];
$found_me = $_POST['found_me'];
(this is the HTML thread)
<label>
<input type="checkbox" name="services[]" value="silver" class="silver">
Silver Pack (Website)
</label>
If you are posting any element with having square brackets [] in the end, you are posting an array of multiple elements.
You are posting checboxes with name services[], so you will get array in posted form.
Also, if you have 10 services checkboxes, only those will be posted which will be checked.
To deal with this HTML:
<input type="checkbox" name="services[]" value="silver" class="silver">
in PHP, use foreach loop for all checkboxes.
<?php
if (! empty($_POST['services'])) {
foreach ($_POST['services'] as $service) {
// YOU ARE GETTING POSTED VALUES HERE.
// NOW, $service will have values like: silver, ...
}
}
?>
This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 4 months ago.
If I have a form:
<form action="/page.php" method="POST">
<input name="length[<?=$ID;?>]['00:12:00']">
</form>
So, on the BACK end, I clearly have an array, but I need to reference the ID number above, i.e.
foreach($_POST['length'] AS $p) {
echo($p['ID']);
echo($p['ID']['length_number']);
}
Is there a way to structure the front end form input data differently so it is easier to comb through on the back end?
You could reference it by using a hidden input field to grab the ID.
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Try using something like this:
foreach($_POST['length'] AS $id=>$values) {
echo $id;
print_r($values);
}
The the above $values will be an array such as array('00:12:00'=>'{value of the input field}')