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");
}
?>
Related
I have 4 different MySQL query's and I want to be able to choose which one is used based on which radio button is clicked. I was wondering what would be the best way to do this?
<form action="">
<input type="radio" name="query1" value="">Rating<br>
<input type="radio" name="query2" value="">Genre<br>
<input type="radio" name="query3" value="">Year<br>
<input type="radio" name="query4" value="">Rating,Genre,Year
</form>
Do I store the query's on individual php pages and then call them using the radio button or ....?
The query's select movie info from a database and then order them by either rating, year, genre or all three.
Set all your radio buttons to hold the same name attribute but with different values, then upon submit and choosing the desired radio button, query for what is matched.
<?php
if(isset($_POST['submit'])){
if(!isset($_POST['query'])){
echo "You chose nothing";
}
if($_POST['query'] == "Rating"){
// query for Rating
echo "Rating";
}
if($_POST['query'] == "Genre"){
// query for Genre
echo "Genre";
}
if($_POST['query'] == "Year"){
// query for Year
echo "Year";
}
if($_POST['query'] == "Rating,Genre,Year"){
// query for Rating,Genre,Year
echo "Rating,Genre,Year";
}
} // brace for if(isset($_POST['submit']))
?>
<form action="" method="post">
<input type="radio" name="query" value="Rating">Rating<br>
<input type="radio" name="query" value="Genre">Genre<br>
<input type="radio" name="query" value="Year">Year<br>
<input type="radio" name="query" value="Rating,Genre,Year">Rating,Genre,Year
<br>
<input type="submit" name="submit" value="Submit query">
</form>
This is at best, a basic example.
When using database-related code, remember to use mysqli with prepared statements, or PDO with prepared statements, they're much safer.
Radio buttons must have same name.
Without a method="post" the <form> method will be "get" so the PHP would use $_GET not $_POST
<form action="">
<input type="radio" name="query" value="1">Rating<br>
<input type="radio" name="query" value="2">Genre<br>
<input type="radio" name="query" value="3">Year<br>
<input type="radio" name="query" value="4">Rating,Genre,Year
</form>
I am not a big fan of using IF ELSE branching structures and avoid them whenever possible.
I prefer passing integer values to be used in arrays.
PHP
$q = intval($_GET['query']);
The intval() will return a zero if ($_GET['query'] return no value.
$queries = array(
0 => $query4,
1 => $query1,
2 => $query2,
3 => $query3,
4 => $query4);
$sql = $queries[$q];
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 will show what a newbie I am. The situation is this. It's a photo contest.
People upload a photo and to the right on the same line is a checkbox.
Voters check the box if they like the photo. They can select up to, say, 5 photos.
Keeping it simple, my problem isn't with MySQL, but with the form. Each row has a checkbox, which is a form. The SUBMIT button is the problem. The only way I can figure out to have submit work is by putting a submit button with each checkbox. Of course, that's ridiculous. What I want is to read all the checked boxes and have ONE submit button when the voter is finished. Spent hours on this and can't see how to have the SUBMIT by itself that the voter can click and have all the checked values inserted into the database a one time.
Any notions? I know this sound very primitive, but just getting into this.
Thanks ahead of time for any help
You can enclose all the checkboxes in a single html form tag and have the submit button inside it. It'll automatically submit data in all checkboxes
Or
You can use javascript/jQuery to run through all the checkboxes. And submit the form.
Well, there are all sorts of javascript ways to go out and grab the data you want and submit it, but let's keep it simple.
[voteform.html]
<form action="process_script.php">
<img src="image1"><input type="checkbox" name="vote_for_image[]" value="1">
<img src="image2"><input type="checkbox" name="vote_for_image[]" value="2">
<input type="submit">
</form>
... then ... (please note the [RETURN ERROR...] and [DATABASE...] blocks are just place holders for you to fill in code)
[process_script.php]
<?
if (count($_GET['vote_for_image'] > 5) {
[RETURN ERROR 'Please select no more than 5 images to vote for']
}
else {
foreach ($_GET['vote_for_image'] as $index => $image_id) {
[DATABASE INSERT OR UPDATE FOR $image_id]
}
}
?>
Set the checkbox[] as name attribute for each checkbox and on submission you can address the valiues as arrays
<body>
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="a">
<input type="checkbox" name="checkbox[]" value="b">
<input type="checkbox" name="checkbox[]" value="c">
<input type="checkbox" name="checkbox[]" value="d">
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['Submit']))
{
echo $_POST['checkbox'];
}
?>
</body>
the above will give you
Array (
[0] => a
[1]=>b
[2] => c
[3]=>d
)
Try this code
//HTML
<form method='post'>
<input type='checkbox' name='photo[]' value='1' />
<img src='test1.jpg' />
<input type='checkbox' name='photo[]' value='2'/>
<img src='test2.jpg' />
<input type='submit' value="Submit" name="submit" />
</form>
//PHP
if(isset($_POST['submit']))
{
if(isset($_POST['photo']))
{
if(count($_POST['photo']) > 5)
{
// Display error msg
}
else
{
// Contains all ids voted on
$img_ids=explode(',',$_POST['photo']);
// insert or update DB for the image ids
}
}
}
Am Newbie.I have this coding...How to store into Mysql and How to Retrieve from the Database?
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
am doubt with if i select both 2 box means what will happen?
Please Explain...Thanks in Advance..
If you have no problem with comma separated values
my answer will help you
simple use array name to your field
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
then implode it with comma
implode(","$_POST['like']);
if two box is checked then
result will be yes,no
make your column in varchar
Then do usual stuff to store
yourscript.php:
<?php
include("mysql_connect.php");
if ($_POST['like'])
{
if ($_POST['like'] != "yes" and $_POST['like'] != "no") die("Hacker");
mysql_query("INSERT INTO likes ('id', 'like') VALUES ('', '".$_POST['like']."')");
}
?>
<form method="post" action="yourscript.php">
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
</form>
retreive.php:
<?php
include("mysql_connect.php");
$likes = mysql_query("SELECT * FROM likes");
while($like = mysql_Fetch_assoc($likes))
{
echo "ID ".$like['id']." => ".$like['like'];
}
If you select both fields, it will save the latest select.
You want radio (probably)
You probably don't want to be doing this in the first place. What you really want to be using is radio buttons for yes/no values. Otherwise, you're going to run into problems.
you can use like this
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
to store in db use serialize(); function.
unserialize() return the same array with keys.
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);