HTML Form POST to PHP show a blank page
Update
PHP Code
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "team44";
$db = mysqli_connect($host, $user, $password, $database);
// Check connection
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
if (isset($_POST['register'])) {
//Get the user info from the form
$uname = $_POST['emailid'];
$pwd = $_POST['passwd'];
$first = $_POST['fname'];
$middle = $_POST['mname'];
$last = $_POST['lname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
echo "username:" . $uname . "passwd:" . $pwd . "first:" . $first . "middle:" . $middle . "last:" . $last . "age:" . $age . "gender:" . $gender;
//Login
$sqlinsert = "INSERT INTO userid (userid, user_name, user_paswd) VALUES (NULL, '$uname', '$pwd')";
// Registation
$datainsert = "INSERT INTO user_data (user_id, first_name, last_name, middle_name, age, sex, userid_fk ) VALUES (NULL,'".$first."', '".$last."', '".$middle."', '".$age."', '".$gender."', '".$id."')";
$insert = mysqli_query($db, $sqlinsert);
$regi = mysqli_query($db, $datainsert);
if (!$insert || !$regi ) {
printf("Error in insert query:%s\n", $insert->error);
}else{
header('location:login.php);
}
}
?>
HTML Code
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Connection">
<meta name="author" content="Suman">
<title>Login or Register </title>
<link rel="stylesheet" href="css/normalize.css" />
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Scope+One" rel="stylesheet">
<link rel="stylesheet" href="css/form.css" />
<link rel="stylesheet" href="css/home.css" />
</head>
<body>
<header>
<h1> Welcome to Connections </h1>
</header>
<section>
<h2> Already a member. </h2>
<form action="login.php" method="post">
<h2> Login </h2> Username
<br/>
<input type="email" value="username" name="uname" /> Password
<br/>
<input type="password" value="password" name="passwd" />
<input type="submit" value="Login" />
</form>
</section>
<section>
<form method="POST" >
<h2> Register </h2> First Name
<input type="text" value="First Name" name="fname" /> Middle Name
<input type="text" value="Middle Name" name="mname" /> Last Name
<input type="text" value="Last Name" name="lname" /> Age
<input type="number" value="18" name="age" />
Gender<br/>
Male: <input type="radio" value="1" name="gender" /> Female:<input type="radio" value="2" name="gender"/><br/>
Email
<input type="email" value="Email" name="emailid" /> Password
<input type="password" value="Password" name="passwd" />
<input type="submit" name="register" value="Register" />
</form>
</section>
<footer> For any queries please contact us
<br /> Phone: +91-40-460870000 ext:1234
<br /> email: admin@gmail.com
<br /> For any issue with the web site please send an email to webmaster@gmail.com
<br/> © Copyright All rights reserved.
<br /> </footer>
</body>
</html>
Sending Form Data Using POST To The Current Page And Another PHP Page At The Same Time
Here is your current page
<?php
$_POST["studioName"] = 'testdata';
$valid = true;
if(empty($_POST["studioName"])){
$studioNameError = " *This Field Cannot Be Empty";
$valid = false;
}
// send data to getstudio.php if valid
if($valid){ ?>
<form method="post" action="getstudio.php" id="senddata">
<input type="hidden" name="postdata" value="<?php print_r($_POST); ?>">
</form>
<script>
document.getElementById("senddata").submit();
</script>
<?php } ?>
Here is your getstudio.php file to receive validated post data
<?php
echo '<pre>';
print_r($_POST['postdata']);
die;
How to simply post javascript data to PHP with HTML form
There is a lot of ways to achieve this. In regards to the way you are asking, with a hidden form element.
change your form to:
<form method="post" name="myform" method="post" action="script.php">
<input type="hidden" name="data" value="">
<input type="submit" name="send" value="submit">
</form>
Javascript:
var data = signaturePad.toDataURL('image/png');
document.myform.data.value = data;
document.forms["myform"].submit();
Or Jquery:
var data = signaturePad.toDataURL('image/png');
$('form input[name="data"]').val(data);
$("form").submit();
HTML/PHP Form Submission (what to do after)
Considering question 1
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
It is not mandatory to have a separate page to display response to the end user. Actually it depends on the scenario for which you are developing the page. The main intention is to provide the best UI experience for the end users / visitors of your site. If there is some crucial information with large content, then it is better to show it in another page after form submission. If it is just a success message then it is fine to stay on the same page after form submit and show the response message there.
Now coming to question 2 and 3
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
You can accomplish it using ajax. With the help of jquery you can make it much simpler using
$.ajax function
Ex :
$("#formbutton").click(function(){
$.ajax({
type: "POST", // method
url: "savecontact.php", // call to external php file
data: {name:n,contact:c}, // passing variables to php file
success: function(result) {
// get the response after ajax call is successful
},
complete: function(result){
}
});
});
Now in savecontact.php you write the necessary code (usual php code to insert into table) to save the data and return response.
Ex:
result = mysqli_query("INSERT INTO table (NAME ) VALUES ('val')"));
if($result)
{
echo "Success";
exit;
}
else
{
echo "Error";
exit;
}
After ajax call gets executed, inside success function of ajax call you will get the response either Success or Error and based on that you can display the message into an empty div which normally placed above the form.
success: function (response) {
if(response == "Success"){
$("#div").html("Thanks, your form was submitted");
}
else{
$("#div").html("Issue with form submission.Please try again");
}
}
The appropriate message will gets display after form submit as ajax response.
Using PHP to return to page after form post
Try this,
<form action="update.php" method="post">
<select name="choice">
<option value="style1" selected>1</option>
<option value="style2">2</option>
<option value="style3">3</option>
</select>
<input type="hidden" name="goback" value="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="submit" value="Go">
</form>
PHP update.php
<?php
$year = 31536000 + time();
setcookie('style', $_POST['choice'], $year);
header('Location:'.$_POST['goback']);
exit();
?>
Related Topics
Using Ajax to Pass Variable to PHP and Retrieve Those Using Ajax Again
Zf2 - Get Controller Name into Layout/Views
PHP Function Imagettftext() and Unicode
Http Response Code After Redirect
Remove Trailing Slash from String PHP
What Is the Maximum Size of an Array in PHP
Pass a Percent (%) Sign in a Url and Get Exact Value of It Using PHP
Mysqli + Xdebug Breakpoint After Closing Statement Result in Many Warnings
Php: Locale Aware Number Format
Does the Order of Class Definition Matter in PHP
Pass Array Literal to Postgresql Function
PHP Files Are Downloaded by Browser Instead of Processed by Local Dev Server (Mamp)
How to Get Page Number on Dompdf PDF When Using "View"
Having Two Different Sessions in Same Domain
Retrieve Data from Db and Display It in Table in PHP .. See This Code Whats Wrong with It