Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="POST">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit">
</form>
</div>
</div>
</body>
</html>
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label
for='.$question_body.'>Yes</label>
<input type="checkbox" name="'.$option_a.'" value="'.$option_a.'">
<input type="hidden" name="'.$question_id.'" value="'.$question_id.'">
<hr>
';
}
}
}
?>
Answer.php
<?php
include 'conn.php';
if(isset($_POST['Submit'])){
if(isset($_POST['answer'])){
print_r($_POST);
}
}
?>
The value checkbox and hidden input did not send any value on the Answer.php page. It did not send any error or warning. 'Option_a' == 'Yes' value and 'question_id' == S1,S2,S3 and so on. Each 'question_id' have their own question and user have to tick if yes. So i want send these value on another page. I hope any of you guys can help me.
I can understand what you actually want to do but not sure what you are actually trying to achieve by saving the options as name and question_id as hidden input.
How i would approach this is saving the question_id as name for the options and i would suggest to use radial buttons if its an yes or no question. Anyways to solve your problem make the following changes to your question format in pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label>'.$question_body.'</label>
<input type="checkbox" name="'.$question_id.'" value="'.$option_a.'">
<input type="checkbox" name="'.$question_id.'" value="'.$option_b.'">
<hr>
';
}
}
}
?>
Now here each question having two options will have the same name corresponding to the question and values as the options. Now your Answer.php should look like
<?php
include 'conn.php';
if(isset($_POST['Submit'])){ // if submit is true
// if you want to print all answers then loop through the question id
// $_POST[$question_id] gives you the value of the answer
//i.e the option the user has chosen for that particular question
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
echo $_POST[$question_id];
}
}
}
?>
Hope this makes sense and helps. :)
check here for more info on checkboxes.
Related
I am working on a school assignment and I have run into some issues. I have PHP code for a form that, when selected, sends the selected result to a MySQL database and then loops through and displays the results. The only problem is that, instead of showing the selected <option>, it shows all four of the options.
Here is my code:
<?php
include_once (connection.php);
if (($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_POST['card_catalog_form']))) {
$card_name = mysqli_real_escape_string($conn, $_POST['card_name']);
$card_label = mysqli_real_escape_string($conn, $_POST['card_label']);
$insert_card_genre_query = sprintf("INSERT into card_catalog (card_name, card_label) VALUES ('%s', '%s')",
$card_name,
$card_label);
$insert_card_genre = mysqli_query($conn, $insert_card_genre_query) or die (mysqli_error($conn));
$last_record = mysqli_insert_id($conn);
}
$card_genre_query = "SELECT card_genre.genre_id, card_label from `card_genre` order by card_label asc";
$card_genre = mysqli_query($conn, $card_genre_query) or die(mysqli_error($conn));
$get_card_genre_query = "SELECT card_catalog.id, card_catalog.card_name, card_catalog.card_label, card_genre.genre_id from card_catalog right join card_genre on card_catalog.card_label = card_genre.card_label";
$get_card_genre = mysqli_query($conn, $get_card_genre_query) or die(mysqli_error($conn));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The Marketplace By The Mana Club</title>
<link rel="stylesheet" type="text/css" href="stylesheets/tmp.css">
</head>
<body>
<?php include(templatestuff/top_of_tmp.php); ?>
<main>
<h1>Products:</h1>
<section>
<ul id="products_list">
<li><b>Product 1: "Jack-In-The-Mox"</b></li>
<li><b>Product Description: "Roll a six-sided die for Jack-in-the-Mox. On a 1, sacrifice Jack-in-the-Mox and lose 5 life. Otherwise, Jack-in-the-Mox has one of the following effects. Treat this ability as a mana source..."</b></li>
<img src="productimages/jackinthemox.jpeg" alt="Jack In The Mox"/>
</ul>
</section>
<div>
<h2>What Card Are You Looking For?</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<fieldset>
<p><b>What's The Card Name? <input type="text" name="card_name"></b></p>
<p>
<p><b>Card Type:</b></p>
<select name="card_genre">
<?php while ($row_card_genre = mysqli_fetch_assoc($card_genre)) { ?>
<option value="<?php echo $row_card_genre['genre_id'];?>"><?php echo $row_card_genre['card_label'];?></option>
<?php } ?>
</select>
</p>
<p><input type="submit"></p>
<input type="hidden" name="card_catalog_form">
</fieldset>
</form>
<?php
if ($last_record) {
echo "<p><b>You just created form query #" . $last_record ."</b><p>";
}
?>
<p>You are submitting your form at
<?php
date_default_timezone_set('America/New_York');
echo date('g:i a \o\n l, F j, Y');
?>
</p>
</div>
<?php
$query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
while($row = mysqli_fetch_array($result)){
echo $row['card_name']. " - ". $row['genre_id'];
echo "<br />";
}
?>
</main>
<?php include('templatestuff/bottom_of_tmp.php'); ?>
</body>
</html>`
(If you want to see the website that contains the problem, you can go here)
Any help, or constructive criticism, would be greatly appreciated.
Thanks
This might point you in the right direction:
if (isset($_POST['card_genre'])) {
$query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre WHERE card_genre.genre_id = ?";
$stmt = mysqli_prepare($conn, $query);
$stmt->bind_param('s', $_POST['card_genre']);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $row['card_name']." - ".$row['genre_id'];
echo "<br />";
}
} else {
$query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($result)) {
echo $row['card_name']." - ".$row['genre_id'];
echo "<br />";
}
}
I am currently working on a school project and I need a little help. I am writing PHP/SQL code for a page where, when the user submits a form, a query runs that loops through and displays the user text input and also the value associated with the <select> dropdown.
(For a visual idea of what I mean, visit http://themanaclub.com/themarketplace2/themarketplace2.php)
Here is my code:
<?php
include_once ('connection2.php');
if (($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_POST['card_catalog_form']))) {
$card_name = mysqli_real_escape_string($conn, $_POST['card_name']);
$card_label = mysqli_real_escape_string($conn, $_POST['card_genre']);
$insert_card_genre_query = sprintf("INSERT into card_catalog (card_name, label_id) VALUES ('%s', %u)",
$card_name,
$card_label);
$insert_card_genre = mysqli_query($conn, $insert_card_genre_query) or die (mysqli_error($conn));
$last_record = mysqli_insert_id($conn);
}
$card_genre_query = "SELECT card_genre.genre_id, card_label from `card_genre` order by genre_id desc";
$card_genre = mysqli_query($conn, $card_genre_query) or die(mysqli_error($conn));
$get_card_genre_query = "SELECT card_catalog.id, card_name, label_id, card_label from card_catalog left join card_genre on label_id = genre_id";
$get_card_genre = mysqli_query($conn, $get_card_genre_query) or die(mysqli_error($conn));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The Marketplace By The Mana Club</title>
<link rel="stylesheet" type="text/css" href="stylesheets2/tmp2.css">
</head>
<body>
<?php include('templatestuff2/top_of_tmp2.php'); ?>
<main>
<h1>Card Input Form:</h1>
<form id="card_name_entry" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<h4>What Card Are You Looking For?</h4>
<textarea name="card_label" rows="5" cols="30" placeholder="Write the name of the card here"></textarea>
<p>
<select name="card_genre">
<?php while ($row_card_genre = mysqli_fetch_assoc($card_genre)) { ?>
<option value="<?php echo $row_card_genre['genre_id'];?>"><?php echo $row_card_genre['card_label'];?><?php echo $row_card_genre['card_name'];?></option>
<?php } ?>
</select>
</p>
<?php $query = "SELECT label_id from `card_catalog` right join `card_genre` on genre_id"; ?>
<p id="textareasubmit"><input type="submit"></p>
<input type="hidden" name="card_catalog_form">
</form>
<section id="all_questions">
<ul>
<?php while ($row_card_genre = mysqli_fetch_assoc($get_card_genre)) { ?>
<li><?php echo $row_card_genre['card_label'];?></li>
<?php }
$row_card_genre = mysqli_data_seek($get_card_genre, 0);
?>
</ul>
</section>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<p id="deletethissubmit"><input type="submit" value="Delete This"></p>
<input type="hidden" name="card_catalog_delete">
<input type="hidden" name="genre_id" value="<?php echo $row_card_catalog['label_id'];?>"
</form>
<p>You're asking this at
<?php
date_default_timezone_set('America/New_York');
echo date('g:i a T \o\n l, F j, Y');
?>
</p>
<p id="backtothemarketplace">Back To The Marketplace</p>
<?php
/*if (isset($_POST['card_genre'])) {
$query = "SELECT card_catalog.card_name, card_catalog.label_id, card_genre.genre_id, card_genre.card_label FROM card_catalog, card_genre WHERE card_genre.genre_id = ?";
$stmt = mysqli_prepare($conn, $query);
$stmt->bind_param('s', $_POST['card_genre']);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $row['card_name']." - ".$row['label_id'];
echo "<br />";
}
}*/
?>
</main>
<?php include('templatestuff2/bottom_of_tmp2.php'); ?>
</body>
</html>
If anybody has any help or constructive criticism, it would be greatly appreciated (I'm a PHP/SQL newbie so I'll take all the help that I can get).
Thanks
Messed around with the code and I fixed it. The id is now being displayed and it links to the correct page.
All that was wrong with my code was that I mixed up some of the variables and ids.
Thank you all for your help.
I displayed a set of questions.
On selecting one of those questions for which I required the answers to be posted on the same page, but the problem is I am not getting id parameter from url which I am passing on selectiong the question.
Now when I am trying to answer the question which is selected, I will need the id of question to post the answer of that perticular question which I already have in url as a parameter for Example: id=1.
Here is the body section of html page:
<?php
include("menu/menu.php");
$sqli = "SELECT * FROM forum_question where id='$id'";
$result=mysqli_query($conn,$sqli);
?>
<form action="submit_answer.php" method="post" name="answers">
<br> <br> <br>
<?php
while($row = mysqli_fetch_array($result))
echo "Q".$row['detail'];
?>
<br>
answers:<br>
<textarea class="tinymce" name="answers"></textarea>
<input type="hidden" name="id" value="<?php echo $id;?>">
<br> <br>
<input type="submit" value="submit" name="submit">
After submit the page "submit_answer.php", Code is:
<?php
include'config.php';
if($conn){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$answers = $_REQUEST['answers'];
$id= $_GET ['id'];
}
$sqli= "INSERT INTO answers (answers)
VALUES ('$answers')";
if (mysqli_query( $conn,$sqli))
{
echo "New record created successfully";
header("location:answer.php?id='$id'");
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}
}else{
}
mysqli_close($conn);
?>
Basically I am very much fresher in Php I just want to know how should I get the id of question and submit it to the "submit_answer.php" with the answer content.
just take a hidden field below the answer field and get the url parameter to that hidden field on page load, as said by user buivankim2020 and submit submit_answer.php,
after submit get the value of that field in variable like what you do for getting the answer..
You should change it (add hidden input for id in markup html)
<?php
include'config.php';
//session_start();
$id= $_GET ['id'];
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="tinymce/js/jquery.min.js"></script>
<script type="text/javascript" src="tinymce/plugin/tinymce/tinymce.min.js"></script>
<script type="text/javascript" src="tinymce/plugin/tinymce/init-tinymce.js"></script>
</head>
<body>
<div id="container">
<div id="main">
<?php
include("menu/menu.php");
$sqli = "SELECT * FROM forum_question where id='$id'";
$result=mysqli_query($conn,$sqli);
?>
<form action="submit_answer.php" method="post" name="answers">
<br> <br> <br>
<?php
while($row = mysqli_fetch_array($result))
echo "Q".$row['detail'];
?>
<br>answers:<br>
<textarea class="tinymce" name="answers"></textarea>
<input type="hidden" name="id" value="<?php echo $id;?>">
<br> <br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
submit_answer.php
<?php
include'config.php';
if($conn){
if (isset($_POST['answers']) && isset($_POST['id'])) {
$answers = $_POST['answers'];
$id= $_POST['id'];
$sqli= "INSERT INTO answers (answers) VALUES ('$answers')";
if (mysqli_query( $conn,$sqli))
{
echo "New record created successfully";
header("location:answer.php?id='$id'");
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}
}
mysqli_close($conn);
}
?>
I have a simple dropdown menu which posts the result to itself but when i choose one of the options in the drop down menu it does not echo back the result as expected.
I'm sure i've just missed out something simple but can't spot it. Any ideas? The form posts but does not echo back $user_settings.
<?php
include "functions.php";
connect();
$sql="SELECT user_id, user_realname FROM users ORDER BY user_realname ASC";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$name=$row['user_realname'];
$options.="<OPTION VALUE=>".$name.'</option>';
}
if(isset($_POST['submit'])){
$user_realname = $_POST['username_select'];
$user_select = mysql_query("SELECT user_id, user_realname FROM users WHERE user_realname = '$user_realname'")
or die ("Could not get user data");
while($row = mysql_fetch_array($user_select)){
$user_settings = $row['user_id'];
echo $user_settings;
}
}
?>
<html>
<head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="POST">
<tr><label>Choose User to Edit</tr>
<tr><SELECT NAME="username_select"><OPTION VALUE=""></option>User's Name<?php echo $options;?></SELECT></label></tr>
<tr><input type="submit" value="submit" name="submit"></tr>
</form>
<?php echo $user_settings;?>
<br/>
Go Back
</body>
</head>
</html>
User $_SERVER['PHP_SELF'] instead of $PHP_SELF
I am this is for a polling system I am making, this code shows the user a list of questions they can pick from:
<div class="main_questions">
<p class="style1 style2"><strong>Select Your Question</strong></p>
<p class="style1">
<form action="vote_list.php" method="post" name="form1" class="style1">
<?php
$sql = "SELECT DISTINCT (question_tba) FROM question ORDER BY answer_id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<p>
<?php echo $row['question_tba']; ?>
<input type="radio" name="questions" value="<?php echo $row['question_tba']; ?>">
</p>
<?php
}
?>
<input type="submit" name="submit" value="Vote">
</p>
</div>
This code should then post the chosen question to this page which allows them to cast a vote:
<?php
include('core/initialise.inc.php');
$q = $_POST['question_tba'];
if (isset($_GET['vote'], $_GET['id'])){
add_vote($_GET['id'], $_GET['vote'], $q);
echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=vote_logged.php\">";
}
?>
</div>
<div class="main_questions">
<p class="style1 style2"><strong>Place Your Vote!</strong></p><?php
foreach (get_answers($q) as $id => $answer){
?>
<p>
<?php echo $answer; ?>
Vote
</p>
<?php
}
?>
</div>
Then when they click on an answer to vote on the functions on this page should increment the chosen answers vote by 1 using these functions:
<?php
function get_answers($q){
$q = $q;
$sql = "SELECT answer_id, answer FROM question WHERE question_tba = '$q'";
$result = mysql_query($sql);
echo "$result";
$answers = array();
while (($row = mysql_fetch_assoc($result)) or die(mysql_error()))
{
$answers[$row['answer_id']] = $row['answer'];
}
return $answers;
}
function add_vote($answer_id, $vote, $q2){
$q2 = $q2;
$answer_id = (int)$answer_id;
$vote = '+';
$sql = "UPDATE question SET answer_votes = answer_votes $vote 1 WHERE question_tba = $q2 AND answer_id = $answer_id";
mysql_query($sql);
}
?>
However, my problem is that when I click on the question I would like to vote on, instead of displaying the answers that I can choose to vote from, it just displays Resource id #6. Can anyone tell me what is wrong with my code?
$result is a resource returned by your mysql_query() call, not an actual row object/array. In the same way that you are using mysql_fetch_assoc() in other areas of your code to extract data, you will need to do this prior to your echo if you want to display the data.
Okay, got this sorted out. Turns out, when posting a radio button value you have to use the name="" instead of value="" pretty easy solution.