POST and GET at the same time - php

hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated
Q.php
<?php
include("pagination.php");
if(isset($res))
{
while($result = mysql_fetch_assoc($res))
{
echo '<div class="shop-item">' ;
echo ' <div class="price">' ;
echo $result['Inquirer'] ;
echo ' </div>' ;
echo ' <div class="price">' ;
echo $result['question'] ;
echo ' </div>' ;
echo ' <div class="actions"> ';
echo '<input type="button" class="btn btn-large " value="More Info" onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
echo '</div> ';
echo ' </div> ';
}
}
?>
QR.php
<form action="QRR.php" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name ="answer" id="Test">
</textarea>
<br>
<div class="actions">
<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>
</div>
</form>
QRR.php
<?php
// variables
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];
//query
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>
if i removed passing hi from QR to QRR answer stored //$answer
if i removed storing answer from the text area the id from url stroed //$FURL

This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.
if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.
You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.
<?php
// variables
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
require ("coonection.php");
$FURL = $_POST['hi']; // there is no input name 'hi' set in your form. so this code will fail due to that.
//query
$query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data){
echo "Your Questions Has Been Successfully Added ";
}
}
?>
`
you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.
<form action="" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>
<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
</div>
</form>
NOTE: both of these snippets will go in the same page.

Related

Set a specific Time duration for page

I would like to specify the duration of the display of a form. Suppose there is a variable with the value x=(10), whenever I call this page, the timer should be based on the current time and the form should be displayed for 10 minutes and after this time the form should be sent automatically.
<?php
include 'db_conn.php';
if (isset($_GET['id'])) {
$var=$_GET['id'];
$query="SELECT * FROM addquiz WHERE quz_id='$var' ";
$db=mysqli_query($conn,$query);
$res=mysqli_fetch_array($db);
$noquestions=$res['noquestion'];
$quiz=$res['title'];
}?>
<div class="container">
<form method="POST" class="form-horizontal" >
<div style="margin-left: 30%;"><b>Your Quiz: <?php echo $quiz;?></b>
<input type="text" name="user_name" placeholder="Enter Your Name" style="height: 30px; width: 30%;"></div>
<div class="row">
<div class="col-md-12">
<div class="panel" style="margin:5%">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="checkbox" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="checkbox" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="checkbox" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="checkbox" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<div class="button" style="margin-left: 60%;"> View Result </div>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
$name=$_POST['user_name'];
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`,`user_name`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "', '";
$query.=$name . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>
It depends on what you are trying to achieve.
As you say "at each visit of the page" it could be done like this with javascript:
window.onload = function () {
window.setTimeout(function () {
document.form.submit();
}, 5000);
};
Further you could set the timeout from your php variable and like the comment says above, you could validate the taken time on the server side with php to make sure no one is cheating.

When Clicked edit button, corresponding details must be filled to the form for edit

I have a form which can submited.and have list,that listed all submited form details.
I tried it in different ways.I want to fill the form with the corresponding details when I clicked the edit button.
Here is my php file
<div class="row">
<div class="col-sm-9">
<b>Leader Name : </b><?php echo($row["lead_name"]); ?><br>
<b>Phone Number : </b><?php echo($row["phone_number"]); ?><br>
<b>Email : </b><?php echo($row["email"]); ?><br>
<b>Part Created Time : </b><?php echo($row["create_date_and_time"]); ?>
<br>
</div>
<div class="col-sm-3 ">
<form role="form" action='index.php' method='POST'>
<input type='hidden' name='party_id' value='<?php echo($row["party_id"]); ?> '>
<input type="submit" class="btn btn-sm btn-success btn-block" id="edit" name="edit" value="Edit" style="font-size: 12px; padding: 3px;">
<input type="submit" class="btn btn-sm btn-danger btn-block" id="delete" name="delete" value="Delete" style="font-size: 12px; padding: 3px;">
</form>
<?php
if (isset($_POST['delete'])) {
print("<script> alert('delete'); </script>");
$party_id = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$queryDelete = "DELETE FROM party_details WHERE party_id='$party_id'";
if ($conn->query($queryDelete)) {
$_SESSION['party'] = "";
$_SESSION['trips'] = [];
print("<script>
alert('Party removed');
window.location.href='../tripCreate';
</script>");
} else {
print("<script>alert('Error when remove ! ');</script>");
}
$_POST = array();
}
if (isset($_POST['edit'])) {
$party_id1 = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$query1 = "SELECT * FROM party_details WHERE party_id='$party_id1'";
$result1 = $conn->query($query1);
$row1 = $result1->fetch_assoc();
}
?>
</div>
first of all, you should specify not only result you want to achieve, but also what kind of problem you are facing.
is it php error, or information not being displayed in resulted page?
one thing i spotted is that you got $row1 = $result1->fetch_assoc(); but in form you echo $row[] (instead of $row1[]), which i dont see being created anywhere.
also, did you try var_dump($row) in php and check its content (or $row1...)?

How to associate query with changing variable in PHP

I'm in need of a bit help. I'm trying to find out how to associate a specific query (deletion of a record) with not the id of a record, but the record with which another query (selection of a record) is echoed out.
This line of code totally works when the id is specified, but again I need it for the record that gets called, where the id can skip numbers if I delete a record.
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
I've got a table in my phpmyadmin database with columns 'id', 'pagetitle', 'toevoeging' (addition in Dutch) , 'message'. First one is an INT, rest are varchars/text.
This may be a stupid question, I'm sorry for that. I'm still new to PHP, and to programming in general.
Here is the code. I've commented on lines code to clarify. Thanks you!.
<?php
if (isset($_SESSION['email'])) //if the admin is active, forms can be written out.
{
echo '</nav>
<br><br> <div class="inlogscript">
<form action="verstuurd.php" method="post">
<input type="text" placeholder="Titel" method="POST" name="pagetitle" /><br><br>
<input type="text" placeholder="Toevoeging" method="POST" name="toevoeging" /><br><br>
<textarea class="pure-input-1-2" placeholder="Wat is er nieuws?" name="message"></textarea><br>
<input type="submit" value="Bevestigen" />
</form></div>';
}
?>
<div class="mainContent">
<?php
include_once("config.php"); //this is the database connection
$query = "SELECT * FROM paginas "; //selects from the table called paginas
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
$pagetitle = $row['pagetitle'];
$toevoeging = $row['toevoeging'];
$message = $row['message'];
echo '<article class="topcontent">' . '<div class="mct">' . '<h2>' . "$pagetitle" .'</h2>' . '</div>' . "<br>" .
'<p class="post-info">'. "$toevoeging" . '</p>' . '<p class="post-text">' . '<br>'. "$message" . '</p>' .'</article>' . '<div class="deleteknop">' . '<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>' . '</div>' ;
} //This long echo will call variables $pagetitle, $toevoeging and &message along with divs so they automatically CSS styled,
//along with a Delete button per echo that has the 3 variables
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked
{
$resulttwo = $mysqli->query($querytwo);
}
?>
</div>
</div>
Also here is the Insert INTO query of the records. Thanks again!
$sql = "INSERT INTO paginas (pagetitle,toevoeging, message)
VALUES ('$_POST[pagetitle]','$_POST[toevoeging]','$_POST[message]')";
//the insertion into the table of the database
if ($MySQLi_CON->query($sql) === TRUE) {
echo "";
} else {
echo "Error: ". $sql . "" . $MySQLi_CON->error;
}
This won't be sufficient but, to begin with your echo :
echo '<article class="topcontent">
<div class="mct">
<h2>' . $pagetitle .'</h2>
</div><br>
<p class="post-info">'. $toevoeging . '</p>
<p class="post-text"><br>'.$message.'</p>
</article>
<div class="deleteknop">
<form method="post">';
// you ll want to use $_POST["id"] array to delete :
echo '<input type="hidden" name="id" value="'.$row['id'].'">
<input name="delete" type="submit" value="Delete Now!">
</form>
</div>' ;

Simple Form does not work

Trying to make a simple form to send an email to a database. New to php. The isset doesn't trigger so i guess the post isn't posting?! Heres what i have tried. I have tried with the get/post require etc. Thanks for any help.
<form class='form-horizontal' action='includes/news-letter.php' method='POST'>
<div class='form-group'>
<div class='col-xs-8 col-xs-offset-2'>
<label for='' class='control-label'>Email</label>
<input type='text' class='form-control input-newsletter' name='newsletterEmail' id='inputPassword' placeholder='someone#somwhere.com'>
</div>
</div>
<div class='form-group'>
<div class='col-xs-6 col-xs-offset-4'>
<button type='submit' class='btn btn-default btn-newsletter' name='newsletterEmail'>Get News Letter</button>
</div>
</div>
</form>
<?php
require_once("dbConnection.php");
news_letter();
function news_letter(){
// if(isset($_POST["newsletterEmail"]))
if (isset($_POST["newsletterEmail"]) && !empty($_POST["newsletterEmail"])){
// print_r($_POST);
echo "IS SET";
$newsletterEmail = $_POST['newsletterEmail'];
echo $newsletterEmail;
if ($newsletterEmail==''){
echo "<h6 class='alert alert-danger'>Please fill in field.</h6><br>";
}else{
$query = "INSERT INTO newsletterEmail ";
$query .= "(newsletterEmail)";
$query .= "VALUES (:newsletterEmail)";
$ps = $db->prepare($query);
$ps->execute(array(
"newsletterEmail" => $newsletterEmail,
));
}
}
};
?>
Your submit button has the same name that your textfield, that why it doesn't work.
You don't have to check either "isset" and "!empty" (not empty) in fact if the value is set it's not empty so just do this :
if(!empty($_POST["newsletterEmail"])){
echo "IS SET";
}

how to create a table in php with mysql?

I am trying to create a table for a leaderboard which displays users high scores for a game. I am using my sql to hold the data and php to get that data and put it into a table. when i go to this page on the site however, the page loads but all the php code shows up instead of the table. i dont know what im doing wrong, how do i get the php to display the table?
Sokoban Game
<body>
<div class="container">
<div class="header">
<h1>Sokoban</h1>
</div>
<div class="nav">
<div class="nav_wrapper">
<ul>
<li>Home</li><li>
Play</li><li>
Help</li><li>
Leaderboards
<ul>
<li>High Scores</li>
<li>Top Levels</li>
<li>Most completed</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="leftSideBar">
</div>
<div class="mainContent">
<h2>High Scores</h2>
<div class = "leaderboard">
<?php
// Get a connection for the database
require_once('../mysqli_connect.php');
// Create a query for the database
$query = "SELECT rankNo, username, highScores FROM Users INNER JOIN leaderboardHighScores";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
echo '<table>
<tr><td><b>Rank</b></td>
<td><b>Username</b></td>
<td><b>High Score</b></td></tr>';
// mysqli_fetch_array will return a row of data from the query
// until no further data is available
while($row = mysqli_fetch_array($response)){
echo '<tr><td>' .
$row['rankNo'] . '</td><td>' .
$row['username'] . '</td><td>' .
$row['highScores'] . '</td><td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
}
// Close connection to the database
mysqli_close($dbc);
?>
</div>
</div>
<div class="rightSideBar">
<h3>Sign in</h3>
<input type="text" placeholder="Username" class="txtBox" /></br>
<input type="text" placeholder="Password" class="txtBox" /></br></br>
<input type="button" value="login" class="btn"/>
<h2>Sign up<h2>
<input type="button" value="Create account" class="btn"/>
</div>
</div>
</body>
</html>

Categories