This question already has answers here:
How to get a form input array into a PHP array
(9 answers)
Closed 4 years ago.
Please tell me there is an n-th amount of input. It is always different, maybe 1, maybe 20. The code looks like this:
<form action="/1.php" method="post">
<input type="text" value="1" name="more[1]">
<input type="text" value="2" name="more[2]">
<input type="text" value="3" name="more[3]">
<input type="text" value="4" name="more[4]">
<input type="text" value="5" name="more[5]">
<input type="submit">
</form>
How to add to the database table as many rows as we have input in the form? That is, 5 records should be created in the table. Thank you in advance.
On your Action page Create a loop on your input array
Try this
foreach($_POST['more'] as $value){
$query = mysqli_query('Insert Into TableName Values("",$value)');
}
Hope this idea will help you
Related
This question already has answers here:
getting multiple checkboxes names/id's with php
(4 answers)
Closed 8 years ago.
I have SQL database which has a table (results) that is gonna save all of the results of my survey.
One of the questions is checkbox (multiple answer), so I made a code that should gather the results of checkbox questions and put its values into the database.
But, something's missing, it overwrites the last value in the array so obviously there is an easy solution which I cannot see. To clarify, I want to store all values of one checkbox question if user checks everything.
HTML FORM
<input type="checkbox" name="checkbox1" value="This is the first value">
<input type="checkbox" name="checkbox1" value="This is the second value">
<input type="checkbox" name="checkbox1" value="This is the third value">
PHP SCRIPT
for($i=1;$i<101;$i++) {
if(isset($_POST['checkbox'.$i])) {
$q[$i] .= $_POST['checkbox'.$i].'; ';
}}
<?php
$db =& JFactory::getDBO();
$query = "INSERT INTO jos_results(question1) VALUES ('$q[1]')";
$db->setQuery($query);
$db->query();
?>
If the name of check box type is same then use array then only it will work.
like here
<input type="checkbox" name="checkbox1[]" value="This is the first value">
<input type="checkbox" name="checkbox1[]" value="This is the second value">
<input type="checkbox" name="checkbox1[]" value="This is the third value">
This question already has answers here:
php: check if an array has duplicates
(17 answers)
Closed 7 months ago.
is there a way to write an if else statement by the input field name?
I have a loop that dynamically creates 9 input field,
The loop
for($i=0; $i<($n*$n); $i++){
echo "<tr>";
for($j=0; $j<($n*$n); $j++){
$number = "column".$i.$j;
if($i%$n==0 && $j%$n==0 && $j!==0 && $i!==0){
echo "<td><input class='field' type='text' name=$number value=$_POST[$number]></td>";
}
and the output is this.
<input class="field" type="text" name="column00" value="1">
<input class="field" type="text" name="column01" value="2">
<input class="field" type="text" name="column02" value="1">
.....
<input class="field" type="text" name="column09" value="3">
So what I am trying to do is, that if there is a inputed number on row 1 to 9 that is equal to another number in row 1 to 9 it will echo out that there more than one number thats equal to each other.
I tried something like this but it wouldn't work.
if($number==$number){
echo = "equal number in the same row";
}
Make an array of all the inputs from a row. Then do:
$unique = array_unique($row_values);
if (count($unique) != count($row_values)) {
echo "No duplicate numbers in a row!";
}
Do the same thing for each column.
Use field names in this form:
<input name="number[<row>][<column>]" ...
So, for instance:
<input name="number[0][0]" value="1" />
<input name="number[0][1]" value="2" />
<input name="number[0][2]" value="5" />
<input name="number[0][3]" value="3" />
...
<input name="number[1][0]" value="4" />
<input name="number[1][1]" value="9" />
When posted, you will receive a two-dimensional array:
[[1, 2, 5, 3, ...], [4, 9, ...]]
Now it basically becomes an array problem you have to solve for:
All rows and columns
Rows and columns within each grid
To help you find the value that occurs multiple times in an array:
$multiples = array_filter(array_count_values($arr), function($freq) {
return $freq > 1;
});
// outputs a non-empty array with the numbers that occur more than once.
print_r(array_keys($multiples));
i am trying to make a online quiz/survey using php and mysql!and im working with php for the very first time!
what im trying to do is take Questions and its multiple choices from my Db(quiz and table Questions with Qid, Qtext, Ans1..Ans4 as its 6 columns) and once the user is done with the quiz n presses the Submit button on last Question.. all the answers should be saved in Db(quiz and table answer with Aid, Ans, Qid as its columns)! i searched for related codes but couldnt understand any of them.
i would be grateful if somebody could help.
thanks.
have inputs as an array in your view, like:
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
and on submit ,
$answers = $_POST['answer'];
foreach($answers as $answer)
{
...
}
I can give you a short idea
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
<input type="submit" name="submit">
//if you have query on another page that is in form action...there is no need of isset
<?php
if(isset($_POST['submit']))
{
$ans=$_POST['answer']; //store in a variable, now this is array of your multiple answer
//iterate it by loop, best is foreach becouse it will continue iteration untill the element //found in array,
foreach($ans as $val)
{
mysqli_query($con, "insert into table_name set answer='$val'") or die("query failed");
}
?>
This question already has answers here:
How to get PHP $_GET array?
(8 answers)
Closed 9 years ago.
<input type="checkbox" name="currency" value="usd"/>
<input type="checkbox" name="currency" value="euro"/>
<input type="checkbox" name="currency" value="cad"/>
Im trying to get currency values through $_GET request, something like /?currency=usd,cad but instead im getting /?currency=usd¤cy=cad
and then $_GET['currency'] returns only one value.
adding name=currency[] just gets /?currency[]=usd¤cy[]=cad
What is the proper way to get these checkbox values in some sort of array?
HTML:
<input type="checkbox" name="currency[]" value="usd"/>
<input type="checkbox" name="currency[]" value="euro"/>
<input type="checkbox" name="currency[]" value="cad"/>
PHP:
<?php
foreach($_GET['currency'] as $currency){
echo $currency."<br/>";
//or what ever
}
?>
Try this way:
name="currency[]"
We have to tell the serverside that this is a name with multiple value.
$_GET['currency'] have to be an array this way.
In your html, make the checkbox name:
<input type="checkbox" name="currency[]" value="usd"/>
This will add the check values to an array. Your method, each check is overwriting the last in your $_GET
I need help ... that too from scratch as now am learning php. just variable declaration i am learning.
I have created a form with 5 check boxes. when i select any 1 or any 2, 3.. or any combination, i should get the data which is already stored for that option in MySQL database.
My form is this:
<form method="post" action="search.php" name="search_form" onsubmit="return checkCheckBoxes(this);">
<input type="checkbox" name="search1" value="qwerty_keypad" id="search1">QWERTY Keypad
<input type="checkbox" name="search2" value="touch_screen" id="search2"> Touch Screen
<input type="checkbox" name="search3" value="usb" id="search3"> SUB Drive
<input type="checkbox" name="search4" value="mobile_tracker" id="search4">Mobile Tracker
<input type="checkbox" name="search5" value="Backup" id="search5">Phone backup on MMC
<input type="submit" value="Search" /> </form>
what i should write in search.php.
Please help me ... please
Thanks in advance
Use an array to submit the values
<input type="checkbox" name="search[connectivity]" value="usb" id="search3"> USB
<input type="checkbox" name="search[display]" value="touchscreen" id="search4">Touchscreen
Afterwards you build your query based on those values:
foreach($_POST['search'] as $k=> $search){
$where[]= $k." = '".mysql_real_escape_string($search)."'";
}
$query = "Select * from table where ".implode(' AND ',$where);