This question already has an answer here:
Dynamic variable names in Javascript
(1 answer)
Closed 9 years ago.
Here is my code currently in javascript:
'methv': document.getElementById("methv").value,
'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value,
in my php file i have a invisible input field named num which has a value of a unique number in my database.
<input id="num" type="hidden" value="<?php echo $post['unique person code']; ?>" />
is there a way to have this value as a variable name with this. For example it will have these values :
'q1' + num : document.getElementById("q1").value
Yes. Use bracket notation:
containerObject['q1' + num] = document.getElementById("q1").value;
Where container object is whatever contains all of your data. num would have to be defined first although.
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:
HTML PHP form entry problem with space?
(2 answers)
Closed 5 months ago.
I am having trouble with the POST method after submitting a form. It is not passing the complete string after submitting the form, but it shows correctly in the "select" & "option" html.
in the database i have a table called "administradores" with a column called "id" and other called "administrador":
id: 1
administrador: "Alejandro Salgado"
this is the php code:
function listAdministradores(){
global $conn;
$result = $conn->query("SELECT * FROM administradores");
while($row = $result->fetch_assoc()){
echo '<option value='.$row["administrador"].'>'.$row["administrador"].'</option>';
}
}
this is the html code:
<form action="./includes/crearpropuesta.inc.php" method="post">
<label for="administrador" class="form-label pt-3">Administrador:</label>
<select name="administrador" id="administrador" class="form-select">
<?php listAdministradores() ?>
</select>
And it shows the name "Alejandro Salgado" correctly:
html result
but when I submit the form and var_dump($_POST["administrador"]), it returns only the first name "Alejandro":
string(9) "Alejandro"
What is going on?
To include characters separated by spaces, you need to enclose the data with quotation marks (in your case double-quotation)
Hence, please change
echo '<option value='.$row["administrador"].'>'.$row["administrador"].'</option>';
to
echo '<option value="'.$row["administrador"].'">'.$row["administrador"].'</option>';
This question already has answers here:
Check if inputs are empty using jQuery
(22 answers)
Closed 5 years ago.
I am trying to check if the number input element is empty using jquery
<input type="number" name="year" id="year" min="1900" max="2017">
Here is an example:
if($("#year").val() == "" || $("#year").val() == null){
//code goes here
}
You just have to target the element by ID, get its value and check if its an empty string or null.
Hope this helps!
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']);
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}')