Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
From the HTML page I am submitting some records to a PHP page, where the records will be saved to the DB and then show a
message saved successfully
message to the user on the same HTML page.
When i submit my form, the records are sent to the HTML page and it displays the success message on another page and not on the same HTML page (where the form code is written). How can i correct it?
HTML
<form action="save.php" class="tsc_form_contact_dark nolabel" method="POST">
<input type="text" name="name" class="form-input" placeholder="Name (required)" required />
<input type="email" name="email" class="form-input" placeholder="Email (required)" required />
<input class="form-btn" type="submit" value="Send Message" />
</form>
PHP
<?php
$name = $_POST["name"];
$email = $_POST["email"];
// Do other stuff
echo "Success";
?>
All you need to do is set a message variable. Something like this:
$msg=isset($_GET['msg']) ? $_GET['msg'] : "";
<div><?php echo $msg; ?></div>
<form action="save.php" class="tsc_form_contact_dark nolabel" method="POST">
//rest of the form
And in the php, you need to redirect to the form page something likt this:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
// Do other stuff
$msg = "Success";
$redirecturl = "form_page.php?msg=".$msg;
header("Location: $redirecturl");
?>
There could be other methods to send result message using session, but I would recommend not doing that.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Background:
I have looked through the variations on this question and solutions, but I have not found one that applies to my situation. I know I'm most likely overlooking something incredibly obvious, but it's been hours and I can't for the life of me pinpoint what it is. I'm following along with this tutorial but making it my own: https://www.taniarascia.com/create-a-simple-database-app-connecting-to-mysql-with-php/.
What happens: When I click the submit button, nothing happens. I see "not set" displayed on the screen when the page loads and after clicking submit, so I know I'm in my else block, which means $_POST is not being set.
What I expect to happen: I expect to see "not set" on page load, but then once I have filled out the form and clicked submit, I expect to see "set" in my echo statement to indicate that I'm in my if statement and isset($_POST['submit']) is true/successful, followed by "Success" indicating that the try code block was successful, plus the values I entered in the form fields appearing in the database. For now, though, I'd just be happy if I got that first "set" to display.
I have tried: I've tried breaking out the php into a separate file and linking it up to the form via action="thatfile.php", but I get the same result. I've also tried using "get" instead of "post" for the method and $_REQUEST instead of $_POST, but again, same outcome - "not set" displayed, no data in the db.
Here is my code:
<?php
if (isset($_POST['submit'])) {
require "../config.php";
echo "set";
try {
$connection = new PDO($dsn, $username, $password, $options);
$new_item = array(
"luggage" => $_POST['luggage'],
"category" => $_POST['category'],
"item" => $_POST['item'],
"description" => $_POST['description']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"stuff",
implode(", ", array_keys($new_item)),
":" . implode(", :", array_keys($new_item))
);
$statement = $connection->prepare($sql);
$statement->execute($new_item);
echo "Success";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "not set";
}
?>
<?php include "header.php"; ?>
<h1>Add An Item</h1>
<form method="post">
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form>
<label for="item">Category</label>
<input type="text" name="category" id="category"></form>
<label for="item">Item</label>
<input type="text" name="item" id="item"></form>
<label for="description">Description</label>
<input type="text" name="description" id="description">
<input type="submit" name="submit" value="submit">
</form>
Back to home
<?php include "footer.php"; ?>
You are closing the form </form> multiple times before the final end of the form </form> so the submit is not in the form. Remove these:
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form> ***HERE***
<label for="item">Category</label>
<input type="text" name="category" id="category"></form> ***HERE***
<label for="item">Item</label>
<input type="text" name="item" id="item"></form> ***HERE***
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need help with creating a form. It can be on blank page, I just need forms on a page.
example : NAME _______
And I would like the filled form be stored on the server so I can print them.
Print example: NAME firstname lastname
Thanks in advance!
Let's say we have two files. The first file, called index.html is a pure-HTML page that displays a form. The second file is called process.php which handles the data from the form server-side.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Example Form</title>
</head>
<body>
<form action="process.php" method="POST">
<label for="firstname_input">First Name:</label><br>
<input type="text" id="firstname_input" name="firstname" /><br>
<label for="lastname_input">Last Name:</label><br>
<input type="text" id="lastname_input" name="lastname" /><br>
</form>
<input type="submit" value="Submit" />
</body>
</html>
process.php:
<?php
if (!isset($_POST['firstname']) || !isset($_POST['lastname']))
{
die("Error! Both the firstname and lastname must be specified.")
}
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
echo sprintf("You specified a first name of '%s' and a last name of '%s'.", htmlspecialchars($firstName), htmlspecialchars($lastName));
From here, you can store $firstName and $lastName in a file or a database (such as MySQL) to store them permanently, but that seems to be beyond the scope of the question. If you choose to store them in MySQL, something like this should work:
$sql = $db->prepare("INSERT INTO user (first_name, last_name) VALUES (:first_name, :last_name)");
$sql->bindValue(':first_name', $firstName);
$sql->bindValue(':last_name', $lastName);
if (!$sql->execute())
{
die("Failed to add user to database.");
}
You didn't specify how or where you wanted to store the data, and there are many other ways to store it, but this is how you'd do it in MySQL.
Here is the code for it :
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
For getting that values in next page u can use ,
action = POST
this parameter in form tag.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working with this issue since last few hours and also searched the related questions on stack overflow. I have a simple html form
<form name="user_verification" action="action.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" name="submit" value="submit">
</form>
and here is the php script in action.php file
if(isset($_POST['submit']))
{
echo 'yes';
}else{
echo 'no';
}
It always display "no". I tested my php script using this
if(1==1)
{
echo 'yes';
}else{
echo 'no';
}
In this case, it displays "yes". This means that problem is with isset($_POST['submit']) function but I can't find out the solution. please help in this regard. thanks
For robustness its best to check the method against the request.
This is a simple example of a form processor validating a post request.
if ('POST' === $_SERVER['REQUEST_METHOD']) {
if (!isset($_POST['required_data'])) {
http_send_status(400);
exit;
}
echo 'OK';
}
You will still need to check with isset against the fields you require.
Maybe somewhere $_POST values are emptied/unseted. This may be due to php configuration or as security measure (i.e. http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading). You may check $_REQUEST and also check if you can get $_GET values (method of form is get).
Your code is correct, it's working for me. See this for more info.
In index.php
<form name="user_verification" action="action.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" name="submit" value="submit">
</form>
in action.php
<?php if(isset($_POST['submit']))
{
echo 'yes';
}else{
echo 'no';
} ?>
The above code will only display the submitted values if the submit button was clicked.
isset( ). This is an inbuilt function that checks if a variable has been set or not. In between the round brackets, you type what you want isset( ) to check. For us, this is $_POST['Submit']. If the user just refreshed the page, then no value will be set for the Submit button. If the user did click the Submit button, then PHP will automatically return a value
$var = '';
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
You may also use the var_dump(isset()); // TRUE
to output the return value of isset().
Your code is correct.
It works only when you submit the form.
So, unless you submit the form, it will always print no.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$redirect=$_GET["r"];
if ( isset($_GET["r"]) ){
header("location: http://" .$redirect);
} else {
$redirect = "mickiewiki.nl/login/profile.php";
header("location: http://" .$redirect);
this code won't work? I want it to be like if I go to mickiewiki.nl/login/login.php?r=mickiewiki.nl/Doneer.php that when I login I go back to the page Doneer.php
When I login, I am always being sent to profile.php no matter if I add my r or not
<?php
//allow sessions to be passed so we can see if the user is logged in
session_start();
//connect to the database so we can check, edit, or insert data to our users table
$database = mysql_select_db('***', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "login/functions.php";
$selectedid = $_GET['id'];
if(empty($selectedid)) {
$selectedid = $_SESSION['uid'];
}
if(strcmp($_SESSION['uid'],"") == 0){
header("location: login/login.php?r=mickiewiki.nl/Doneer.php");
} else {
?>
In your form on your website, you are not setting r through GET. You have username, password & submit for your form - and nothing for GET. If you are submitting r with your form (which I cannot see that you are), then you should probably be checking for $_POST['r'] instead of $_GET['r'].
Basically, you need to add a hidden field for your r variable and submit that along with your form.
<form method="post" action="login.php">
<input type="hidden" name="r" value="<?php print $_GET['r']; ?>">
<p><label for="name">Username: </label>
<input type="text" name="username" /></p>
<p><label for="pwd">Password: </label>
<input type="password" name="password" /></p>
<p>
<input type="submit" id="submit" value="Login" name="submit" />
</form>
but, you must do a control for $_GET["r"] ... and why it doesn't work?
I am new to PHP, I tried to work w3 schools example of posting data on forms..
It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!
Example #1: A simple contact from - HTML code
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2: Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Expected output of this script may be:
Hi Joe. You are 22 years old.
Actual Output:
Hi . You are years old
The Post parameter is not displaying data.. Any help is really appreciated.
What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.
However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)
If you wish to make use of htmlspecialchars, do the following in your welcome.php file:
<?php
$fname = htmlspecialchars($fname);
?>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.
Form used:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname );
And placed within <?php and ?> tags such as:
<?php
$fname = htmlspecialchars( $fname );
?>
NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO
mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.
Please visit this answer
StackOverflow did not let me insert the codes on that page, for one reason or another.
In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?
//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
.
//action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT 2
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
'post' or 'POST' both works fine in form tag.
The following should be in action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT
and check whats the error.
Most probably it should work now...
In your form check if it is not sending empty values.
STEP 1
copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.
The following code should be in index.php
<form action="action.php" method="POST" onsubmit="return validate()">
<p>Your name: <input type="text" name="name" id="name"/></p>
<p>Your age: <input type="text" name="age" id="age"/></p>
<p><input type="submit" /></p>
</form>
<script type="text/javascript">
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
alert ("Name="+name+" age="+age);
return false;
}
</script>
This code will check if the values are getting entered correctly without redirecting the page to action.php.
Step 2
If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
if (name==null || name==""){
return false;
}
if (age==null || age==""){
return false;
}
return true;
}
If both name and age are filled in the form, the submit will now redirect to action.php
Step 3
In action.php use the following code.
<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>
edited with instructions on OP's request
Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding