Post text box value when checkbox is selected - php

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']; } ?>

Related

How to get multiple checked checkbox ID and VALUE at the same time

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/>';
}

Get results of multdimensional array from form post

So I have a form which is build up like:
<input name="website['menu']['id']" type="number" value="1">
<input type="text" value="#000000" name="website['color']['primary']['hex']">
Now i want to read out those values in php after they are submit.
I tried the following code, but when I var_dump it it gives me null.
if (isset($_POST['website'])) {
$result = $_POST['website'];
var_dump($result['menu']['id']);exit;
}
Remove the quote from field name
<input name="website[menu][id]" type="number" value="1">
<input type="text" value="#000000" name="website[color][primary][hex]">
And access the variable in php like:
echo $_POST['website']['menu']['id'];
Try this,
<?php
print_r($_POST['website']);
print_r($_POST['website']['menu']['id']);
?>
<form action="" method="POST"/>
<input name="website[menu][id]" type="number" value="1">
<input type="text" value="#000000" name="website['color']['primary']['hex']">
<input type="submit" value="submit"/>
</form>

PHP error - Inserting a user form into a database

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);
?>

Using Checkbox Values in PHP

I have this page (code below) which contains a series of 5 checkboxes, all representing different values, and i need to use them to query a database.
<form id="form1" name="form1" method="post" align="center" action="section_search_results1.php">
<p>Select The <b>Section</b> You Wish To Search In Below... </p>
<p>
<label for="section"></label>
<label for="section2"></label>
<label>
<input type="checkbox" name="SectionSelect" value="Functional" id="SectionSelect_0" />
Functional</label>
<label>
<input type="checkbox" name="SectionSelect" value="Technical" id="SectionSelect_1" />
Technical</label>
<label>
<input type="checkbox" name="SectionSelect" value="Commercial" id="SectionSelect_2" />
Commercial</label>
<label>
<input type="checkbox" name="SectionSelect" value="Implementation" id="SectionSelect_3" />
Implementation</label>
<label>
<input type="checkbox" name="SectionSelect" value="Innovation" id="SectionSelect_4" />
Innovation</label>
<br />
</p>
<p>Enter the <b>Keyword(s) or Keyphrase</b> Below...</p>
<p>
<label for="kword"></label>
<input type="text" name="kword" id="kword" placeholder="Enter Keyword(s)" />
<br />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Search" /> | | <input type="reset" name="clear" id="clear" value="Clear" />
</p>
</form>
as you can see each has its own value which is the term used to query the database. the php query code on the results page is as follows
<?php
$kword = $_POST["kword"];
$section = $_POST["SectionSelect"];
function boldText($text, $kword) {
return str_ireplace($kword, "<strong>$kword</strong>", $text);
}
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and section_name like '%$section%'
and keywords like '%$kword%';")
or die(mysql_error());
?>
ultimately what i want this to do is to query the database with the query to potentially have where clauses for each of the checkbox values? for example i want to select x where y like 'Technical' AND y like 'Functional' and so on...
Any help would be great
thanks
Set up your checkbox names as array-keys to use them as an array with values of all selected checkboxes:
<input type="checkbox" name="SectionSelect[0]" value="Functional" />
<input type="checkbox" name="SectionSelect[1]" value="Technical" />
echo $_POST['SectionSelect'][0]; // should print "Functional"
echo $_POST['SectionSelect'][1]; // should print "Technical"

Auto submit data to MySQL?

I am teaching myself code, and after going over PHP & MySQL tutorials, I'm still a little unsure.
I want to create a page in which the user ticks relevant checkboxes, saves the data, and can log back in another time and the ticks are saved.
I've learned how to use data that is in MySQL, but how is data auto-submitted by the user? That's got me stumped...
You need to use a form:
http://www.tizag.com/htmlT/forms.php
<form method="post" action="/your/php/script.php">
Name: <input type="text" size="10" maxlength="40" name="name"> <br />
Password: <input type="password" size="10" maxlength="10" name="password">
<input type="submit" value="Save"/>
</form>
This isn't really a question than can be answered simply, but here's the Simple answer.
First, put your checkboxes in a form:
<form action="page2.php" method="post">
<input type="checkbox" name="cb1" value="SomeValue">SomeValue</input>
</form>
Then, in page2.php, put the data (which is in the $POST array) into the MySql database using the mysql* functions (mysql_connect, mysql_select_db, mysql_query, etc).
Here is an example.
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<form action="" method=post>
<b>Company Name</b> <input name="CompanyName" type="text" value="<?php echo $row['company'] ?>" /><br>
<b>First Name</b> <input name="firstname" type="text" value="<?php echo $row['firstname'] ?>"/><br>
<b>Last Name</b> <input name="lastname" type="text" value="<?php echo $row['lastname'] ?>" /><br>
<input type="submit" value="Submit">
</form>

Categories