replicate php readline() array in browser [duplicate] - php

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

Related

Store values of multiple checkboxes in one array and send it to another PHP page [duplicate]

This question already has answers here:
Multiple inputs with same name through POST in php
(5 answers)
Closed 1 year ago.
I have a while loop that displays 4 checkboxes from $choices with value answer_id from database.
<?php while($row=mysqli_fetch_assoc($choices)){ ?>
<input type="checkbox" name="choices[]" value="<?php echo $row['answer_id']; ?>"><?php echo $row['choice']; ?><?php } ?><?php } ?>
<input type="submit" name="submit" value="submit">
I want to send this array of selected choices to another page and store each of its value in different variables.
if(isset($choices)){
$choice1id = $_POST['choices'];
}
if(isset($choices)){
$choice2id = $_POST['choices'];
}
if(isset($choices)){
$choice3id = $_POST['choices'];
}
if(isset($choices)){
$choice4id = $_POST['choices'];
}
If user has selected 2 checkboxes so its value gets on index 0 and 1 in array and these 2 values get stored in variables choice1id and choice2id on another PHP page.
But it says Warning: Undefined variable $choice1id, $choice2id etc.
how do I solve this?
I'm not sure if this is what you wanted. Seems like you want to get $_POST['choices'] and create variables from it? If so, first check if it was posted, then loop through each one. You can create dynamic variables in the loop. This will give you variables like $choice1id $choice2id .. etc
if(isset($_POST['choices'])){
$ctr = 1;
foreach ($_POST['choices'] as $choice) {
$var = "choice" . $ctr . "id";
$$var = $choice;
$ctr++;
}
}

PHP: How to pass multiple parameters under same name through url? [duplicate]

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/>

Pushing value into the session array in php [duplicate]

This question already has answers here:
Fatal error: [] operator not supported for strings
(9 answers)
Closed 6 years ago.
I have the following code where the user selects the value from a drop down. Everytime they submit, the ID should be pushed in the array.
<?php
$_SESSION["test"] = array();
$link = mysql_connect("localhost","root","");
mysql_select_db("dbsikkim",$link);
?>
<form name="form1" method="post" action="">
<?php
$sql=mysql_query("SELECT ID,DOC_DESC FROM gstn_document_type_master",$link);
echo "<select name='doc_type_code2'>";
if(mysql_num_rows($sql)){
while($row=mysql_fetch_array($sql)){
echo "<option value=".$row['ID'].">".$row['DOC_DESC']."</option>";
}
}
echo "</select>";
?>
<input type="submit" value="Submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$_SESSION["test"][] = $_POST['doc_type_code2']; //ERROR IN THIS LINE
for ($i=0; $i < sizeof($_SESSION["test"]); $i++) {
echo $_SESSION["test"][$i]."<br>";
}
}
?>
Only the current value is pushed into the array and replaces the previous one. In simple words, it is working as a variable and not an array.
You most likely need to get your session variable initialized.
$_SESSION["test"] is not an array by default. You could try to do:
// If the session variable is not yet an array, initialize it
if (!is_array($_SESSION["test"])) {
$_SESSION["test"] = [];
}
$_SESSION["test"][] = $_POST['doc_type_code2'];
The first line assigns an array to your field.
The second one assigns a value to the array.
Please note, there are several things to improve in your code. In example, you could use PDO these days. Also you should not use "global". Also mixing PHP and HTML code like that might lead to software which is hard to maintain. I don't want to sound arrogant, but I highly recommend you the book "PHP The right way".

php- how to insert form input in an array? [duplicate]

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

Associative PHP Array with Key and Value in PHP Form [duplicate]

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}')

Categories