I'm a beginner.I have displayed student id and student name of 10 students on a table. Against each student id, there should be a checkbox(dynamic). When I click the ADD button, all the checked students details (id,name) must be inserted into another database table. What should i do?
Use checkbox name as an array,
example :
<form method="post" action="" id="frm_id">
<input type="checkbox" name="chkid[]" value="10,Anu" />Anu
<input type="checkbox" name="chkid[]" value="11,Raj" />Raj
<input type="checkbox" name="chkid[]" value="12,Ram" />Ram
<input type="checkbox" name="chkid[]" value="13,xxx" />xxx
<input type="checkbox" name="chkid[]" value="14,yyy" />yyyy
<input type="checkbox" name="chkid[]" value="15,zzz" />zzz
<input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq
<input type="submit" value="Insert" name="sub"/>
</form>
<?php
if(isset($_POST['sub']))
{
$id=$_POST['chkid'];
for($i=0;$i<count($id);$i++)
{
$exp=explode(',',$id[$i]);//Explode id and name
echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>";
echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>";
}
}
?>
<form method="post" action="pageurl">
<input type="checkbox" name="studentid[]" value="1,Student1" />Student1
<input type="checkbox" name="studentid[]" value="2,Student2" />Student2
<input type="checkbox" name="studentid[]" value="3,Student3" />Student3
<input type="checkbox" name="studentid[]" value="4,Student4" />Student4
<input type="submit" />
</form>
<?php
$id=$_POST['studentid'];
foreach($id as $student)
{
$extract = explode(',',$student);
$query="INSERT INTO student (id,name) values ('$extract[0]','$extract[1]')";
}
?>
try using the array of checkbox element like this:
<input type="checkbox" name="months[]" value="feb">February<br>
<input type="checkbox" name="months[]" value="mar">March<br>
<input type="checkbox" name="months[]" value="apr">April<br>
Related
How to get by POST multiple checked checkbox IDs and VALUEs at the same time? Code bellow shows html to send data.
<form action="" method="post">
first<input type="checkbox" name="first[]" id="first'<?php echo $data; ?>'" value="first" />
second<input type="checkbox" name="first[]" id="second'<?php echo $data2; ?>'" value="second" />
third<input type="checkbox" name="first[]" id="third'<?php echo $data3; ?>'" value="third" />
<input type="submit" value="submit">
</form>
After send by post I get values, but ID is missing.
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value.'<br/>';
}
How can I send ID and VALUE and get them by post without explode them? For sure i can split them after, but there should be another way.
You could do something like:
<form action="" method="post">
first<input type="checkbox" name="first[0][value]" id="first[]" value="first" />
<input type="hidden" name="first[0][id]" value="first[]">
second<input type="checkbox" name="first[1][value]" id="second[]" value="second" />
<input type="hidden" name="first[1][id]" value="second[]">
third<input type="checkbox" name="first[2][value]" id="third[]" value="third" />
<input type="hidden" name="first[2][id]" value="third[]">
<input type="submit" value="submit">
</form>
And on the back-end:
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value['value'].'<br/>';
echo 'ID: '.$value['id'].'<br/>';
}
If you want to get an id value from an input, used the id as key in your name array
<input type="checkbox" name="first[first]" .../>
<input type="checkbox" name="first[second]" .../>
<input type="checkbox" name="first[third]" .../>
or
<input type="checkbox" name="first[1]" .../>
<input type="checkbox" name="first[2]" .../>
<input type="checkbox" name="first[3]" .../>
then when you loop over your posted inputs, include the key in the key=>value
foreach($_POST['first'] as $id => $value){
echo 'ID: '.$id.' => VALUE: '.$value.'<br/>';
}
<form action="#" method="post">
<input type="checkbox" name="box[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="box[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="box[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
I am using php code to fetch checked box values,
how can I save each checked values in different variable out of loop?
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['box'])){
foreach($_POST['box'] as $selected){
echo $selected."</br>";
}
}
}
?>
I'm getting a strange error when I try to submit user-generated data to a database via PHP commands. When I hit the submit button below, instead of the PHP page running its' function I am presented with a display of the raw code on my browser. I have a command at the bottom of my HTML page that looks like this:
<form action="insert.php" method="post">
<input type="submit">
</form>
So that when the user hits the submit button, the PHP file insert.php (detailed below) is called to input the answers onto a database, separating each answer into it's own field.
Here is the code I'm working with:
<?php
$con=mysqli_connect("host","username","password","database");
// Check connection
if (mysqli_connect())
{
echo "Failed to connect to MySQL: " . mysqli_errno();
}
$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Right now, the questions are in a and not a (is there a functional difference in this case?). They look like:
<form class="testAns" id="widthAns">
<input type="radio" name="answer" value="skinny">-25%
<input type="radio" name="answer" value="skinny">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="fat">+10%
<input type="radio" name="answer" value="fat">+25%
</form>
<form class="testAns" id="spaceAns">
<input type="radio" name="answer" value="small">-25%
<input type="radio" name="answer" value="small">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="wide">+10%
<input type="radio" name="answer" value="wide">+25%
</form>
<form class="testAns" id="weightAns">
<input type="radio" name="wanswer" value="light">-25%
<input type="radio" name="answer" value="light">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="heavy">+10%
<input type="radio" name="answer" value="heavy">+25%
</form>
<form method="post" action="insert.php" class="testAns" id="heightAns">
<input type="radio" name="answer" value="short">-25%
<input type="radio" name="answer" value="short">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="tall">+10%
<input type="radio" name="answer" value="tall">+25%
</form>
The important part is for the "value" associated with each button to be logged into the database. For example, if a user selects "+10%" I want be able to log the word "heavy".And then there are two text input fields:
<form id="intro">
City: <input type="text" name="answer"><br>
Why you are using this tool:<input type="text" name="answer">
</form>
So for these text fields I need the user input logged as the answer.
I see you got the PHP thing fixed.
Now you need to fill your form with data. This:
<form action="insert.php" method="post">
<input type="submit">
</form>
sends only the submit value. You need to add input fields inside that form tag, otherwise, nothing else will get sent. So, since you're sending an answer array, you should add those (adding them as text fields, as an example):
<form action="insert.php" method="post">
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
etc...
<input type="submit" />
</form>
And make sure you filter all user inputs before writing anything into the database, as otherwise my buddy Bobby Tables might come to visit you.
Make sure in your XAMPP Control Panel that Apache and MySQL are running. Then check if your input fields are inside the <form action='insert.php' method='POST'> input fields </form>
Your HTML code would look like this:
<html>
<body>
<form action='insert.php' method='POST'>
<table>
<tr><td>Width: </td><td>
<input type="radio" name="width" value="skinny">-25%
<input type="radio" name="width" value="skinny">-10%
<input type="radio" name="width" value="mid">normal
<input type="radio" name="width" value="fat">+10%
<input type="radio" name="width" value="fat">+25%
</td></tr>
<tr><td>Spacing: </td><td>
<input type="radio" name="spacing" value="small">-25%
<input type="radio" name="spacing" value="small">-10%
<input type="radio" name="spacing" value="mid">normal
<input type="radio" name="spacing" value="wide">+10%
<input type="radio" name="spacing" value="wide">+25%
</td></tr>
<tr><td>Weight: </td><td>
<input type="radio" name="weight" value="light">-25%
<input type="radio" name="weight" value="light">-10%
<input type="radio" name="weight" value="mid">normal
<input type="radio" name="weight" value="heavy">+10%
<input type="radio" name="weight" value="heavy">+25%
</td></tr>
<tr><td>Height: </td><td>
<input type="radio" name="height" value="short">-25%
<input type="radio" name="height" value="short">-10%
<input type="radio" name="height" value="mid">normal
<input type="radio" name="height" value="tall">+10%
<input type="radio" name="height" value="tall">+25%
</td></tr>
<tr><td>City: </td><td><input type="text" name="city"></td></tr>
<tr><td>Why you are using this tool: </td><td><input type="text" name="tool"></td></tr>
<tr><td></td><td><input type='submit'></td></tr>
</table>
</form>
</body>
</html>
What are you using in creating your php files? Dreamweaver? Notepad? Try this: SAVE AS your file, Save As Type: All Files and name it insert.php.
<?php
$con=mysqli_connect("localhost","YourUsername","YourPassword(if any)","NameOfYourDatabase");
// Check connection
if (mysqli_connect())
{
echo "Failed to connect to MySQL: " . mysqli_errno();
}
$width=$_POST['width'];
$spacing=$_POST['spacing'];
$weight=$_POST['weight'];
$height=$_POST['height'];
$city=mysqli_real_escape_string($con,$_POST['city']);
$tool=mysqli_real_escape_string($con,$_POST['tool']);
/* REAL ESCAPE STRING WOULD PREVENT A BIT OF SQL INJECTION */
$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$city','$tool','$width','$height','$spacing','$weight')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
I want to post the text box value to the database when checkbox is checked and the form is submitted,how can i do this.My code snippet is,
Html:
<form name="form1" method="post" enctype="multipart/form-data">
<td>Accessories with computer<br>
Power Adapter<input type="checkbox" name="accessories[]" value="power adapter"><br>
Power Cord<input type="checkbox" name="accessories[]" value="power cord"> <br>
Monitor<input type="checkbox" name="accessories[]" value="monitor"><br>
Keyboard<input type="checkbox" name="accessories[]" value="keyboard"><br>
Mouse<input type="checkbox" name="accessories[]" value="mouse"><br>
<input type="text" size="10" name="others1" class="input-text" />
<input type="checkbox" name="accessories[]" value="<?= $_POST['others1'];?>" ><br>
<input type="text" size="10" name="others2" class="input-text" /><input type="checkbox" name="accessories[]" value="<?=$_POST['others2'];?>" ><br>
</td>
</tr>
</form>
php:
$accessories = implode(' ', $_POST['accessories']);
$query ="INSERT INTO ticket SET accessories = '$accessories'";
$result = mysql_query($query);
I've also tried passing the checkbox value as a variable but value is not stored in database. Please suggest me.
the section of the form looks like,
You mixed insert query with update query. The query should look like this
INSERT INTO ticket (acessiories) VALUES ('$acessories')
<form name="form1" method="post" enctype="multipart/form-data">
<input type="text" size="10" name="others2" class="input-text" onclick="document.form1.submit();" />
</form>
<?php
if(isset($_POST))
{
//Do insert query to insert into database
}
?>
I think you just forgot to mention the " action='filename.php' " in the form tag.
hey refer following code,
<form method="post"> <input type="checkbox" name="check" /> <input type="text" name="txt" /> <input type="submit" name="submit"/> </form>
<?php $a=$_POST['checkboxname']; if($a) /* true when checkbox is checked */ { echo $_POST['textboxname']; } ?>
i want insert this form value to datanase :
<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>
these text box are get by php from a table in database and may be Variable
i want insert to database by this format
if brand 1 are checked $brand="1,";
and Finally like this :
insert($name,$brands); and $brands = "1,2,3,4,5,";
if write this by if and while but it doesn't work because if insert run in while {} Five times insert Done and if insert run out of while {} , $brand = "5,"
thanks for your help or idea for this problem
it's mean :
<form method="post" action="#">
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
?>
<input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
<?php }} ?>
Source Output:
<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>
when submit form , insert source is :
<?php
$result = $db->getall(brands);
if(!empty($result)) {
while ( list($key,$val)=each($result) ) {
$brand_id = brand.stripslashes($val["id"]);
$brand_name = stripslashes($val["name"]);
$brand_ids = "brand.$brand_id";
if($$brand_ids==1) {$brands="$brandid,"}
}} ?>
$db->add_submenu("$brands");
You should change the name of your checkboxes to brand[]. It will give you an array once submitted at $_POST['brand']
Ex.
<input type="checkbox" name="brand[]" value="1" ... />
<input type="checkbox" name="brand[]" value="2" ... />
<input type="checkbox" name="brand[]" value="3" ... />
<input type="checkbox" name="brand[]" value="4" ... />
<input type="checkbox" name="brand[]" value="5" ... />
on the other side you can either do something like the following:
// this will return '1, 2, 3, 4, 5' when all are selected.
$index = implode(", ", $_POST['brand']);
and at that point you will have the brands in comma delimited form.