inserting status for four inputs from select php - php

I have four HTML inputs and a select option. i want to insert the status for the three inputs = "0" and for the selected one = "1". kindly help me and tell some thing about mysql query for
<input placeholder="Choice A:" name="a">
<input placeholder="Choice B:" name="b">
<input placeholder="Choice C:" name="c">
<input placeholder="Choice D:" name="d">
<select class="form-control" name="select">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
i want the status for a is 1 and for the remaing 0 in Database :

I don't know what you want to achieve but as per your comments you are looking for this:
HTML:
<form method="post" action="">
<input type="text" placeholder="Choice A:" name="a">
<input type="text" placeholder="Choice B:" name="b">
<input type="text" placeholder="Choice C:" name="c">
<input type="text" placeholder="Choice D:" name="d">
<select class="form-control" name="select">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<input type="submit" name="submit" value="Submit Now">
</form>
PHP:
if(count($_POST) > 0){
$updateZero = array();
$updateOne = array();
$selectArr = array('A','B','C','D'); // default array
foreach ($selectArr as $key => $value) {
if($value == $_POST['select']){
$updateOne[] = $value; // store those selected
}
else{
$updateZero[] = $value; // store those not selected
}
}
echo "<pre>";
print_r($updateOne); // selected array
print_r($updateZero); // remaining array
}
Result:
Array
(
[0] => A
)
Array
(
[0] => B
[1] => C
[2] => D
)
Now you can use these array in MYSQL Statement as you need.

Related

how to insert into database with multiple array in php

hi guys can someone help me how to this. I have here the problems I can't solve it
for example
data for manny
<input type="text" name="name[]" value="Manny" >
<input type="text" name="course[]" value="BSIT" >
<input type="text" name="level[]" value="2nd year" >
<input type="text" name="course[]" value="BSCS" >
<input type="text" name="level[]" value="3rd year" >
<input type="text" name="course[]" value="BSENG" >
<input type="text" name="level[]" value="4th year" >
data for Floyd
<input type="text" name="name[]" value="Floyd" >
<input type="text" name="course[]" value="ABM" >
<input type="text" name="level[]" value="Grade 11" >
<input type="text" name="course[]" value="STEM" >
<input type="text" name="level[]" value="Grade 12" >
Manny hold 3 classes while Floyd hold 2 classes. so how can I insert it into database I guess use an array is suit for this problem but I don't know how. please help me with this
I think it will make more sense to use an array if the structure "data for Manny" and "data for Floyd" are the same. Not like when you are having "GRADE" in "data for Floyd" and "YEAR" in "data for Manny".
Moreover, what exactly are you trying to achieve? You don't want to treat Manny and Floyd data separately?
You can have a check and see if this is going to help.
Manny Data
<input type="text" name="Manny[name]" value="Manny"><br>
<select name="Manny[year1]">
<option value="BSIT">BSIT</option>
</select><br>
<select name="Manny[year2]">
<option value="BSCS">BSCS</option>
</select><br>
<select name="Manny[year3]">
<option value="BSENG">BSENG</option>
</select><br>
Floyd Data
<input type="text" name="Floyd[name]" value="Floyd"><br>
<select name="Floyd[Grade 11]">
<option value="ABM">ABM</option>
</select><br>
<select name="Floyd[Grade 12]">
<option value="STEM">STEM</option>
</select><br>
=====RESULT=====
Array(
[Manny] => Array
(
[name] => Manny
[year1] => BSIT
[year2] => BSCS
[year3] => BSENG
)
[Floyd] => Array
(
[name] => Floyd
[Grade 11] => ABM
[Grade 12] => STEM
)
)
$_POST["Manny"] contains all Manny's data.
$_POST["Floyd"] contains all Floyd's data.

Multiple option select form

I have created a HTML form where a user can enter his availability (which day of week). The form has a button to add new users so at the end I will have multiple DIVs for users, hence I have USER_DOW with two dimensions USER_DOW[][].
<div id="user1" class="user" >
<div class="name">
<label>Name</label>
<input type="text" name="USER_Name[]">
</div>
<div>
<label>Day of Week</label>
<select multiple id="USER_DOW" name="USER_DOW[][]" size='7'>
<option value="Mon">Monday</option>
<option value="Tue">Tuesday</option>
<option value="Wed">Wednesday</option>
<option value="Thu">Thursday</option>
<option value="Fri">Friday</option>
<option value="Sat">Saturday</option>
<option value="Sun">Sunday</option>
</select>
</div>
</div>
I'm having issues accessing the elements in the PHP
foreach($USER_Name as $a => $b){
echo $a+1;
echo $USER_Name[$a];
echo "Number of selected days for user " + count($USER_DOW);
foreach($USER_DOW as $c => $b){
echo $USER_DOW[$c][$a];
}
}
At the moment, if I add 2 users, one selecting Wed.& Sun. and the second only Mon., what I get is three days (count) for both users and on the first user all three are printed (Wed, Sun, Mon) while for the second nothing.
Any idea? Have I misunderstood the keys in the arrays?
The names of input elements of a form are passed as written, without any interpretation.
So, in this example:
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<?php if( count($_GET) ): ?>
<pre><?php var_dump($_GET); ?></pre>
<?php endif; ?>
/* FORM #1 */
<form action="test.php">
<input type="text" name="txt[0]" value="One">
<input type="text" name="txt[1]" value="Two">
<input type="submit" name="action" value="Test">
</form>
/* FORM #2 */
<form action="test.php">
<input type="text" name="txt[]" value="One">
<input type="text" name="txt[]" value="Two">
<input type="submit" name="action" value="Test">
</form>
/* FORM #3 */
<form action="test.php">
<input type="text" name="txt[][]" value="One">
<input type="text" name="txt[][]" value="Two">
<input type="submit" name="action" value="Test">
</form>
<?php
?>
</body>
</html>
The generated URLs are:
/* FORM #1 */ test.php?txt[0]=One&txt[1]=Two&action=Test
/* FORM #2 */ test.php?txt[]=One&txt[]=Two&action=Test
/* FORM #3 */ test.php?txt[][]=One&txt[][]=Two&action=Test
(rawurldecoded for clarity)
When process $_GET/$_POST variables, PHP try to interpreter it, so in the first and second forms, the result is the same:
Array
(
[txt] => Array
(
[0] => One
[1] => Two
)
[action] => Test
)
But, in form #3, the result is:
Array
(
[txt] => Array
(
[0] => Array
(
[0] => One
)
[1] => Array
(
[0] => Two
)
)
[action] => Test
)
because PHP increment first-level key, but not the deeper key.
If you expectation is to increment the deeper array, you have to specify first-level key in the form.
Something like:
<input type="text" name="txt[1][]" value="One">
<input type="text" name="txt[1][]" value="Two">
or, if code-generated, like:
<input type="text" name="txt[<?php echo $SomeVar; ?>][]" value="One">
<input type="text" name="txt[<?php echo $SomeVar; ?>][]" value="Two">
<?php
var_dump($_POST);
function do_form($index) {
?>
<div id="user<?= $index ?>" class="user" >
<div class="name">
<label>Name</label>
<input type="text" name="USER_Name[<?= $index ?>]">
</div>
<div>
<label>Day of Week</label>
<select multiple id="USER_DOW" name="USER_DOW[<?= $index ?>][]" size='7'>
<option value="Mon">Monday</option>
<option value="Tue">Tuesday</option>
<option value="Wed">Wednesday</option>
<option value="Thu">Thursday</option>
<option value="Fri">Friday</option>
<option value="Sat">Saturday</option>
<option value="Sun">Sunday</option>
</select>
</div>
</div>
<?php
}
?>
<form method="POST">
<?php do_form(1); do_form(2); ?>
<input type="submit">
</form>
Sample output after submit:
array (size=2)
'USER_Name' =>
array (size=2)
1 => string 'foo' (length=3)
2 => string 'bar' (length=3)
'USER_DOW' =>
array (size=2)
1 =>
array (size=1)
0 => string 'Mon' (length=3)
2 =>
array (size=2)
0 => string 'Mon' (length=3)
1 => string 'Tue' (length=3)

How to identify a form field type in php

<form name="form" action="" method="get">
<input type="text" name="name" id="name" value="My name">
<textarea name="about_me" id="about_me"></textarea>
<input type="radio" name="gender" value="male" />
<input type="radio" name="gender" value="female" />
<select name="level">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Expert">Expert </option>
</select>
</form>
I have form like this,
My form fields are dynamically added.
After submitted the form, i need to identify the field type, needs to know as name is textbox value, about_me is textarea input, gender is radio option, level is dropdown etc..
is that any way to find out form field type in php.
First: take MyWay's approach. It is straight-forward and dead simple. However, if you want to build up a more complex structure, you could use the following code. It sets up hidden fields as an array and holds the name and type, separated by a ::
HTML Page:
<form name="form" action="" method="get">
<input type="hidden" name="fields[]" value="name:text">
<input type="hidden" name="fields[]" value="about_me:textarea">
<input type="hidden" name="fields[]" value="gender:radio">
<input type="hidden" name="fields[]" value="level:select">
<input type="text" name="name" id="name" value="My name">
<textarea name="about_me" id="about_me"></textarea>
<input type="radio" name="gender" value="male" />
<input type="radio" name="gender" value="female" />
<select name="level">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Expert">Expert </option>
</select>
</form>
Afterwards, in your PHP file:
$fields = $_POST["fields"];
foreach ($fields as $field) {
list($name, $type) = explode(':', $field);
$val = (!empty($_POST[$name]))?$_POST[$name]:"";
if ($type == "textarea") {
// do sth. useful with it
// the value is in $val (if there's one)
}
}

fill 2 forms one time php

I have one form like this
Arrivo:
<select name="arrivo">
<option value="2014-06-15">domenica 15 Giugno</option>
<option value="2014-06-16">lunedì 16 Giugno</option>
<option value="2014-06-17">martedì 17 Giugno</option>
Partenza: <select name="partenza">
<option value="2014-06-15">domenica 15 Giugno</option>
<option value="2014-06-16">lunedì 16 Giugno</option>
<option value="2014-06-17">martedì 17 Giugno</option>
<option value="2014-06-18">mercoledì 18 Giugno</option>
Adulti:
<select name="PER">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<INPUT TYPE = "Submit" VALUE = "Calcola Preventivo">
</form>
I need that some data (like ARRIVO E PARTENZA) will auto-filled in another form (that will be created after a user click on submit), that will be added a new field that user must complete. See this example (where name and email are the new field that user must fill in the second form)
Arrivo:
<input type="text" name="arrivo2" value='$_POST['arrivo']><br>
Partenza
<input type="text" name="partenza2" value='$_POST['partenza']><br>
email
<input type="text" name="email"><br>
Name
<input type="text" name="name"><br>
<INPUT TYPE = "Submit" VALUE = "emailme">
form 1
<form method="post">
<input type="text" name="name" />
<input type="hidden" name="hidname"/>
</form>
after submitting this form (i assume you know process for submitting a form) get the post values in session variable like this
session_start();// this starts a session
$_SESSION['name']=$_POST['name'];
$_SESSION['hidname']=$_POST['hidname'];
from 2
<?php session_start(); ?>
<form method="post">
<input type="text" name="name2" value="<?php if(isset($_SESSION['name'])) echo $_SESSION['name'];?>"/>
<input type="text" name="hidname2" value="<?php if(isset($_SESSION['hidname'])) echo $_SESSION['hidname'];?>" />
</form>

foreach loop doesn't work properly

I really don't know what is wrong with this script.. I really thought everything was right, but somehow my strpos syntaxs doesn't work properly or something. The $_POST['category'] is a select list with multiple selections permitted. So that's why I put it in an array, but maybe it is incorrect?
$cat_array = $_POST['category'];
foreach($cat_array as $key => $value )
{
if(strpos($value, 'n_') !== false)
{
// Do something about the new categories.
} else {
// work with existing categories
}
}
the html - I also have a jquery that handles the add category fields. The n_(number)-(value) is created by jquery.
<div>
<label for="category">Category</label>
<select name="category" size="10" multiple="MULTIPLE">
<option class="cat_1" value="1">Cars</option>
<option class="cat_2" value="2">Lego</option>
<option class="cat_3" value="3">Country</option>
<option class="cat_4" value="4">School</option>
<option class="cat_5" value="5">Cooking</option>
<option class="cat_6" value="n_6-test">test</option>
<option class="cat_7" value="n_7-Buuh">Buuh</option>
</select> <br>
<input type="text" name="new_cat" value="" size="40" maxlength="120" placeholder="Category Name"><input class="plus" name="" type="button" value="Add Category">
<p class="plus_comment"></p>
</div>
Your $_POST['category'] is not an array. After seeing your var_dump result, it must be a string only. If it as an array, the var_dump should display like this,
array (size=1)
1 => string '5' (length=1)
So, check your HTML code whether it passes an array.
EDIT: Change the category to category[]
try to your select list name
category[]
To make your category actually an array you have to append brackets to the category like this: category[]
So your HTML woul look like this
<div>
<label for="category">Category</label>
<select name="category[]" size="10" multiple="MULTIPLE">
<option class="cat_1" value="1">Cars</option>
<option class="cat_2" value="2">Lego</option>
<option class="cat_3" value="3">Country</option>
<option class="cat_4" value="4">School</option>
<option class="cat_5" value="5">Cooking</option>
<option class="cat_6" value="n_6-test">test</option>
<option class="cat_7" value="n_7-Buuh">Buuh</option>
</select> <br>
<input type="text" name="new_cat" value="" size="40" maxlength="120" placeholder="Category Name">
<input class="plus" name="" type="button" value="Add Category">
<p class="plus_comment"></p>
</div>

Categories