PHP Radio button mysql query - php

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];

Related

JQuery Validation With Multiple Sets Of Radio Buttons

I have a form that did submit 1 out of 8 radio buttons to a php $_POST super global array. Therefore, I needed some validation. I was kindly helped by being provided this code which works great:
$("#form").submit(function (event) {
if(!$(":radio:checked").length) {
alert("You must select at least one emotional state!");
event.preventDefault();
}
});
However, I have now been asked to have 2 sets of 8 radio buttons in which the user selects 2 answers instead of the initial 1 answer. I need the code to be able to determine that at least one radio button from each set of 8 buttons has been selected before the form is submitted. At the moment the code checks to see if any radio buttons have been selected, then as soon as just 1 buttons is selected, the function is satisfied and transitions to the next page, which is not what I want.
EDIT
Buttons code:
<p><input type="radio" value="happy" name="perceived_emotion">Happy
<input type="radio" value="excited" name="perceived_emotion">Excited
<input type="radio" value="angry" name="perceived_emotion">Angry
<input type="radio" value="frustrated" name="perceived_emotion">Frustrated
<input type="radio" value="miserable" name="perceived_emotion">Miserable
<input type="radio" value="sad" name="perceived_emotion">Sad
<input type="radio" value="tired" name="perceived_emotion">Tired
<input type="radio" value="relaxed" name="perceived_emotion">Relaxed</p>
<p><input type="radio" value="happy" name="induced_emotion">Happy
<input type="radio" value="excited" name="induced_emotion">Excited
<input type="radio" value="angry" name="induced_emotion">Angry
<input type="radio" value="frustrated" name="induced_emotion">Frustrated
<input type="radio" value="miserable" name="induced_emotion">Miserable
<input type="radio" value="sad" name="induced_emotion">Sad
<input type="radio" value="tired" name="induced_emotion">Tired
<input type="radio" value="relaxed" name="induced_emotion">Relaxed</p>
Here is the form code:
<form id="form" action="audio_handler.php?id=1" method="POST">
<div id="perceived_emotions">
<?php include("includes/induced_emotion_buttons.php"); ?>
</div>
<br />
<div id="induced_emotions">
<?php include("includes/perceived_emotion_buttons.php"); ?>
</div>
<p class="right"><input type="submit" name="submit" value="Submit"></p>
</form>
What you want to do is use the [name="value"] selector in JQuery in conjunction with the :checked selector. So your new code would be:
if(!$('input[name="perceived_emotion"]:checked').length) {
alert("You must select at least one perceived emotional state!");
event.preventDefault();
return false;
}
if(!$('input[name="induced_emotion"]:checked').length) {
alert("You must select at least one induced emotional state!");
event.preventDefault();
return false;
}
All of that wrapped into the form event.
EDIT: Since you only want to display one dialog at once, just add return false; in each if statement.

Redirecting to the other page after submitting radio button option

I have a problem with my form. I need it to redirect user to different pages basing on which radio button was selected. User has to choose one of two options and click next, and according to his choice, page should redirect him to other page.
Here is the code as it looks for now
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio2" value="Firma"/>Company
</br>
<input type = "submit", class = "buttonStyle2", value=""/>
</form>
</center>
</fieldset>
and then php code
if(isset($_POST['Company'])
header("Location: http://myaddress.com/company.php");
Big thanks in advance for your help
Here is one way to achieve this.
Sidenote: Make sure you're not outputting before header. Consult this page on Stack about possible Headers already sent..., should this occur and making sure error reporting is set/on.
Otherwise, PHP will fail silently.
if(isset($_POST['radio1']) && ($_POST['radio1']) == "Osoba fizyczna"){
header("Location: http://www.example.com/non_company.php");
}
elseif(isset($_POST['radio1']) && ($_POST['radio1']) == "Firma"){
header("Location: http://www.example.com/company.php");
}
else{
header("Location: http://www.example.com/redirect_to_home.php");
}
Nota: The else would be if the person did not make a choice and simply clicked on submit without making a selection.
while using radio buttons of the same group name in your form:
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
Note about
<input type = "submit", class = "buttonStyle2", value=""/>
remove the commas
<input type = "submit" class = "buttonStyle2" value=""/>
Since HTML source in FF will reveal No space between attributes in red/as an error.
<fieldset>
<legend>Select option</legend>
<center>
<form method="post" action="">
<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company
</br>
<input type = "submit" class = "buttonStyle2" value=""/>
</form>
</center>
</fieldset>
and
if ( isset($_POST['radio1']) ) {
$filename = $_POST['radio1'] . "php";
header("Location: http://myaddress.com/".$filename);
}
Might be even better to set up an array with allowed values and check if radio1 is in that array.

Having a different action for each radio button in a form

I was wondering if it is possible to make each radio button in a form link to a different action php page? say if I had 2 radio buttons, one named 'basketball' and one named 'football' would I be able to have them link to different php? here is my code;
<form action="football.php" method="post">
<p>Please select your first Sport:</p>
<input type="radio" name="sport" value="football">Football<br></input>
<input type="radio" name="sport" value="basketball">Basketball<br></input>
<input type="radio" name="sport" value="tennis">Tennis</input>
<br><input type="Submit" name="Submit" value="Submit"></form>
Is there any reason they need to be distinct pages?
form.php
<form action="sport.php" method="post">
<p>Please select your first Sport:</p>
<input type="radio" name="sport" value="football">Football<br></input>
<input type="radio" name="sport" value="basketball">Basketball<br></input>
<input type="radio" name="sport" value="tennis">Tennis</input>
<br><input type="Submit" name="Submit" value="Submit"></form>
sport.php
<?php
if (isset($_POST["sport"])
&& $_POST["sport"] == "football") {
//logic specific for football
} else if (isset($_POST["sport"])
&& $_POST["sport"] == "basketball") {
//logic specific for basketball
} else if (isset($_POST["sport"])
&& $_POST["sport"] == "tennis") {
//logic specific for tennis
} else {
//die or some kind of error handling can be done
}
?>
If they absolutely need to be different pages, you can do something like the below:
sport.php
<?php
echo "<meta http-equiv='refresh' content='0;url=./dir/subdir/".$_POST["sport"].".php'/>";
//so if posted form data == football, redirect to football.php, etc
?>
Sure it lacks finesse, but you won't be able to get your desired outcome otherwise, unless you use jQuery/JS.

submitting multiple ans with singl submit button in mysql

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

Searching and retrieving value from database through checkbox selection

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

Categories