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"
Related
I am in a silly position where I can't figure out to get the values of the checked checkboxes.
<form id="civilForm" method="POST" action="form.php" enctype="multipart/form-data">
<p>
<label>
<input id="12D" name="programsRequested[]" type="checkbox" />
<span>12D</span>
</label>
</p>
<p>
<label>
<input id="xp" name="programsRequested[]" type="checkbox" />
<span>XPStorm</span>
</label>
</p>
<p>
<label>
<input id="autoTurn" name="programsRequested[]" type="checkbox" />
<span>AutoTurn</span>
</label>
</p>
<p>
<label>
<input id="hecras" name="programsRequested[]" type="checkbox" />
<span>HEC RAS</span>
</label>
</p>
Then I am using a php loop as there are a bunch more checkboxes coming.
It spins through fine, but only give me a list that says: on, on, on which correctly tells me how many I checked, however does not give me the value of the checked box.
$selectedPrograms = 'None';
if(isset($_POST['programsRequested']) && is_array($_POST['programsRequested']) && count($_POST['programsRequested']) > 0){
$selectedPrograms = implode(', ', $_POST['programsRequested']);
}
Is there something obvious I missing on how to get the values here?
add every input element value
<input id="12D" name="programsRequested[]" type="checkbox" value="1" />
form not closed .
submit button also missing .
<input type="submit" name="submit" >
i want get the values of form in other php file but i am getting only one input value as the name are same in loop....How to get all the input values with the same name or if i have to change the name tell me how? and how to get it in other php file
<?php
if(isset($_POST['submit'])){
//getting values of number of MCQ questions and true/false questions
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
$number = 1;
echo '<form action="finalquestions.php" method="post">';
//loop to get inputs of mcq as many as the user posted
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$number.'<input type="text" name="question1" /> </br></br>';
echo '<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>';
echo '</br></br>';
$number++;
}
//loop to get inputs of truefalse as many as the user posted
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$number.' <input type="text" name="question2" /> </br></br>';
echo '<input type="radio" name="question2">True</br>
<input type="radio" name="question1">False</br>';
echo '</br></br>';
$number++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>
I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..
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']; } ?>
firstly sorry my english is bad
then
i have problem
i try to make lopping insert into table but i have problem i can't make array for inputs
to can insert in table with looping from another table
maybe u can't understand me but look to the code and u will understand my problem
$select = mysql_query("SELECT * FROM table_name ");
?>
<form method="post" action="">
<?
while($row =mysql_fetch_array($select))
{
if($_POST['add'])
{
$updpol = mysql_query("insert into table_name2 (yes,no,maybe,g_id)
values
('".$_POST['yes']."','".$_POST['no']."','".$_POST['maybe']."','".$row['id']."')
")
}
else{
?>
<input type="checkbox" name="yes" value="1" />
<br />
<input type="checkbox" name="no" value="1" />
<br />
<input type="checkbox" name="maybe" value="1" />
<?
}
?>
<input type="submit" name="add_poll" value="submit" />
</form>
in your php code , your checking if:
if($_POST['add'])
While there's no input named add in your form.
I believe you meant:
if($_POST['add_poll'])
In order to have multiply instances of inputs , you can do something like:
<input type="checkbox" name="yes[]" value="1" />
<br />
<input type="checkbox" name="no[]" value="1" />
<br />
<input type="checkbox" name="maybe[]" value="1" />
So , now , when posted:
$_POST['yes'] is an array of all the checkboxes.
And as an array , it starts with $_POST['yes'][0] and continues $_POST['yes'][1] and so on.
Therefore:
$select = mysql_query("SELECT * FROM table_name ");
?>
<form method="post" action="">
<?
$i = 0;
while($row =mysql_fetch_array($select))
{
if($_POST['add_poll'])
{
$updpol = mysql_query("insert into table_name2 (yes,no,maybe,g_id)
values
('".$_POST['yes'][$i]."','".$_POST['no'][$i]."','".$_POST['maybe'][$i]."','".$row['id']."')
");
$i++;
}
else{
?>
<input type="checkbox" name="yes[]" value="1" />
<br />
<input type="checkbox" name="no[]" value="1" />
<br />
<input type="checkbox" name="maybe[]" value="1" />
<?
}
?>
<input type="submit" name="add_poll" value="submit" />
</form>