Redirect to URL after form post - php

I have a problem which i can't resolve. I want to redirect to an URL after i do a form post (button clicked). How can i do that? I didn't find a solution so far on the internet.
My files:
addmatch.html
<html>
<body>
<form action="insert.php" method="post">
Date: <input type="text" name="date">
Home: <input type="text" name="home">
Away: <input type="text" name="away">
Result: <input type="text" name="result">
<input type="submit" name="submit">
</form>
</body>
</html>
insert.php
<?php
define("HOST", "localhost");
define("DBUSER", "..");
define("PASS", "..");
define("DB", "..");
$con=mysqli_connect(HOST, DBUSER, PASS, DB);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO wedstrijden (date, home, away, result)
VALUES
('$_POST[date]','$_POST[home]','$_POST[away]','$_POST[result]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Thanks in advance!

as I can see now, you form will send post request to insert.php
if you want to redirect user after you will save data in database you should add header("location youUrl.php"); before any output (echo, print etc)

Related

Prevent mysql query without input

I have a piece of code which inserts user's input into a database:
Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("DB status: connection failed: " . $conn->connect_error);
} else {
echo "DB status: connected";
}
?>
<html>
<h1>Add data</h1>
<form method="post">
<p>Name: <input type="text" name="name"></p>
<p>Goals scored in:</p>
<p>14/15 <input type="text" name="14"></p>
<p>15/16 <input type="text" name="15"></p>
<p>16/17 <input type="text" name="16"></p>
<p>17/18 <input type="text" name="17"></p>
<button type="submit" name="save">save</button>
</form>
<?php
$sql = "INSERT INTO `goals` (`Name`, `14/15`, `15/16`, `16/17`, `17/18`) VALUES ('".$_POST["name"]."', '".$_POST["14"]."', '".$_POST["15"]."', '".$_POST["16"]."', '".$_POST["17"]."')";
$result = mysqli_query($conn,$sql);
?>
The problem is that when I load the page for the first time, it already sends 0's to the database. How can I prevent this from happening?
Thanks a lot for helping!
add an action to your form and use that to send the sql query. You should probably also be using form validation requiring some fields like name to be filled out.
<p>Name: <input type="text" name="name" required></p>
Calling a particular PHP function on form submit
Add this piece of code at the beginning to fix the issue:
if (isset($_POST['submit']))
{
}

Pre Complete HTML Form with PHP and MYSQL

I am creating a simple page which updates a single record tempKey=1, single field reqdTemp MySQL dBase. I have the form working fine; it updates the record, then returns to the initial form ready for the user to change the temperature again.
Q: I would like the form to be pre-populated with the existing information from the database so the user sees the current required temperature about to be changed. I'm not sure where to start!!
The form, updateTemperature.php, is this:
<html>
<body>
<h1>RPi BBQ - Set Temperature</h1>
<form action="insert.php" method="post">
<p>Set Temperature: <input type="text" name="setTemp" /></p><br><br>
<input type="submit" value="Set Temperature" />
</form>
</body>
</html>
The post script, insert.php is this:
<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo 'Connected successfully';
$sql = "UPDATE PiBQ_Temp SET reqdTemp = '$_POST[setTemp]' WHERE tempKey = 1";
mysqli_query($con,$sql);
echo "1 record added";
header ('location: PiBQ_Temp2.php');
mysql_close($con)
?>
To pre-populate the form, query the database for the current value and set that in the returned HTML. So your updateTemperature.php could become something like this:
<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo 'Connected successfully';
$currentTemp = 100; // some default
$sql = "SELECT reqdTemp FROM PiBQ_Temp WHERE tempKey = 1";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$currentTemp = $row['reqdTemp'];
}
mysql_close($con);
?>
<html>
<body>
<h1>RPi BBQ - Set Temperature</h1>
<form action="insert.php" method="post">
<p>Set Temperature: <input type="text" name="setTemp" value="<?= $currentTemp ?>" /></p><br><br>
<input type="submit" value="Set Temperature" />
</form>
</body>
</html>

HTTP Error. Invalid Method

<?php
if(isset($_POST['SubmitButton']))
{
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql="INSERT INTO app (fname, lname) VALUES ('$_POST[fname]','$_POST[lname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
mysql_close($con)
?>
<html>
<body>
<form action="" method="post">
App Name: <input type="text" name="fname" /><br><br>
App ID: <input type="text" name="lname" /><br><br>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
This is a basic form where I want to keep the user on the same page after submission.
I used this tutorial
php form - on sumbit stay on same page
to help me, but I get:
HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.
Can someone tell me what mistake I made?

Submitting html data and staying on same page

I have a form page
<html>
<body>
<form action="insert.php" method="post">
App Name: <input type="text" name="fname" /><br><br>
App ID: <input type="text" name="lname" /><br><br>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
And this is the php page:
<html>
<body>
<?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql="INSERT INTO app (fname, lname) VALUES('$_POST[fname]','$_POST[lname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
Now, I want it to display 1 record added on the same page as the form and not have the form send me to a new insert.php page with it. Basically, I want the submit form to stay in the same page with maybe a a new message popping up to show that it has worked.
I have already looked through some answers on stackoverflow like using
if(isset($_POST['SubmitButton']))
but it doesn't work. Maybe I placed it wrong or used it incorrectly but could someone help me figure it out?
Just make it an action to itself, and set an if $_POST['submit'] to add the records, and you can have both in the same file. If you wish to do so without refreshing the page, you'll need to use AJAX.
<html>
<body>
<?php
if (isset($_POST['SubmitButton'])) {
$con = mysql_connect("xxx","xxx","xxx");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$sql="INSERT INTO app (fname, lname)
VALUES
('$_POST[fname]','$_POST[lname]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
}
?>
<form action="" method="post">
App Name: <input type="text" name="fname" /><br><br>
App ID: <input type="text" name="lname" /><br><br>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Also, mysql_* is deprecated, so you should consider converting to PDO or MySQLi to avoid SQL injection!

Posting form input value to the same page to build SQL connect string

I am trying to make a form's input value the trigger for a database connection; can anyone tell me what I am doing wrong please?
I thought I could post the value and on the page load after submission use the posted value as a variable to complete my connect string.
Code:
<form name="form" action="Test.php" method="post">
<B>Please Enter Password</B> <input type='password' name='test-' id ='password123';/>
<button id='test'>Submit Year</button>
</form>
<?php
$password= $_POST['password123'];
$conn = #mysql_connect('localhost','root','$password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('ct', $conn);
?>
Try this
<?php
if ($_POST['do'] == 'something') {
$conn = mysql_connect('localhost','root','$password') or die(mysql_error());
mysql_select_db('ct', $conn);
//Uncomment the below to prevent refresh spam
//header("Location: {$_SERVER['REQUEST_URI']}");
//die();
}
?>
<form name="form" action="?process" method="POST">
<input type='hidden' name='do' value='something'/>
<strong>Please Enter Password</strong>
<input type='password' name='test-' id ='password123'/>
<button id='test'>Submit Year</button>
</form>
Consider using MySQLi also.
try this
<form name="form" action="<?php $_SERVER['PHP_SELF']?>" method="post">
<B>Please Enter Password</B> <input type='password' name='test-' id ='password123';/>
<button id='test' name="submit">Submit Year</button>
</form>
<?php
if(isset($_POST['submit'])) {
$password= $_POST['test-'];
$conn = #mysql_connect('localhost','root','$password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('ct', $conn);
}
?>

Categories