how to write isset() function? - php

it is only displaying not set everytime I click on the submit button. I am unable to submit the form this is a sample code my actual code is also not working nor this. if there is something wrong with my system please help me with is too.
<?php
if(isset($_POST['submit'])){
echo "Button clicked";
} else{
echo "not set";
}
?>
<html>
<head>
<title>Sample page</title>
</head>
<body>
<form name="form1" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

The default value of a form's method attribute is GET. The data is being submitted in the query string where it will be available through the $_GET superglobal.
To move the data to the request body, where it will be available through the $_POST superglobal, you need to specify method="POST" as an attribute on the <form>.

Related

Echo textarea's value with PHP

I want to echo the textarea value with PHP, so I create a simple form with HTML, and inside it I include textarea element with name of b64_place and then input to submit the values.
I check if b64_place is set, and if it is I echo the value of the textarea. But my program doesn't even get into the condition block, I try debugging and it is just not doing nothing.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="index.php" method="GET">
<textarea name="b64_place" form="encode">Enter text here:</textarea>
<input type="submit" value="Encode">
</form>
<?php
if (isset($_GET['b64_place'])) {
$base64e_text = htmlspecialchars($_GET['b64_place']);
echo $base64e_text;
}
?>
</body>
</html>
Your textarea contains an attribute form This attribute is used to define the id of the form this input is attached to. So, when you submit the form, the textarea isn't bound with that form and the datas aren't send
You can either add an id to the form :
<!-- check this ----------------------v---------v -->
<form action="index.php" method="GET" id="encode">
<textarea name="b64_place" form="encode">Enter text here:</textarea>
<input type="submit" value="Encode">
</form>
or simply remove the form="encode"
Edit based on suggestion from senior SO members,
The reason i recommend you to change the method to POST is because of the length limit of the GET method. At some point you may want to encode very large data and it may get trimmed of because of URL length limit. But with POST you don't have to worry about this restriction.
Steps to solve your issue.
If your Form and your PHP code is in the same file changethe action="index.php" to action="" and change the method="GET" to method="POST"
In text area use placeholder to tell the user what to input instead of writing it between the tags.
change $_GET to $_POST everywhere in your code.
You can copy the following code into the index.php and it will work fine.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<textarea name="b64_place" placeholder="Enter text here:"></textarea>
<input type="submit" value="Encode">
</form>
<?php
if (isset($_POST['b64_place'])) {
$base64e_text = htmlspecialchars($_POST['b64_place']);
echo $base64e_text;
}
?>
</body>
</html>

PHP form not sending data to $_POST

<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post">
<input name="test" type="text">
<input type="password" name="data">
<input type="submit">
</form>
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['test'];
$pass = $_POST['data'];
}
?>
I am trying to make a simple form in PHP that users will use for login. When using this code, nothing ever gets put inside the $_POST. I am debugging using PHPstorm and I can tell that the request method is definitely POST, but no data is getting passed through. What am I doing wrong?
Try adding a slash at the begening of the action parameter of the form tag.
<form action="/test.php" method="post">
Try to remove "action" from form. It worked for me.
Code: http://nimb.ws/6pXjnz
Output:http://nimb.ws/PvuxZs

HTML Button that saves and move to the next page

I have a small problem. I want to have a button in my html page that saves every data that is added in the textfields and also when I click it to move to the next page.
My code is the follow...
<input type=button onClick="location.href='education.php'" value='Next'>
but it only moves to next page it does not save the data in the database ...
Can you help me please?
Thanks.
Remove the JavaScript
Change the type to submit
Wrap it in a <form>
Set the action of the form to education.php
Set the method of the form to post
Then, in education.php, read the data from $_POST and use PDO (with bound variables) to insert it into the database.
Try this :
<?php
if(isset($_POST['submit']))
{
// Insert Query Put here
header('Location: education.php');
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="Next" name="submit">
</form>
</body>
</html>
education.php :
<?php
echo "Successfully Updated.";
?>
You will have to set an action to your form like below because you are not submitting the form, but just redirection to another page without taking the form data.
<form action="education.php" method="post">
<!-- All your input fields here -->
<input type="submit" name="submit" value="Next">
</form>
and your education.php should be look like this:
<?php
//Get all parameters using $_POST
//Make A connection to database
//Choose a database in which you have to save the data
//Create a SQL query
// run query using mysql_query($query);
//Redirect to anywhere with header("Location:page.php");
?>

Check in PHP if form was submitted when used javascript to submit the form

I have this code :
<html>
<head>
<title>Title</title>
</head>
<body>
<form name="selectForm" id="selectForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="checkbox" name="checkbox" form="selectForm" />CheckBox
</form>
<input type="button" value="Submit" name="submit" onclick="document.selectForm.submit();" />
<?php
if(isset($_POST['submit'])) {
echo "Submitted";
} else {
echo "Not submitted";
}
?>
</body>
</html>
I can not detect if the form was submitted. I would like to modify the PHP code and not the html, if possible.
Previously I had my Submit type input inside the form and it worked, but now that it is outside and I use JavaScript to do the submit it does not work.
How can I detect if the form is submitted ?
The $_POST['submit'] will no longer exist, since the submit button is no longer part of the form. Instead we can check the $_SERVER['REQUEST_METHOD'].
if($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'Submitted';
}
Also, if you leave the form's action attribute blank, it will submit to the current page:
<form name="selectForm" id="selectForm" action="" method="post">
Update:
Add a hidden field with the name of your form:
<input type="hidden" name="formname" value="selectForm" />
<?php
if(isset($_POST['formname'])) {
echo $_POST['formname'] . ' submitted';
}
?>

PHP form - on submit stay on same page

I have a PHP form that is located on file contact.html.
The form is processed from file processForm.php.
When a user fills out the form and clicks on submit,
processForm.php sends the email and direct the user to - processForm.php
with a message on that page "Success! Your message has been sent."
I do not know much about PHP, but I know that the action that is calling for this is:
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
How can I keep the message inside the form div without redirecting to the
processForm.php page?
I can post the entire processForm.php if needed, but it is long.
In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.
For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.
Like this:
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
The best way to stay on the same page is to post to the same page:
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
There are two ways of doing it:
Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the form action to the current page URL.)
if(isset($_POST['submit'])) {
// Enter the code you want to execute after the form has been submitted
// Display Success or Failure message (if any)
} else {
// Display the Form and the Submit Button
}
Using AJAX Form Submission which is a little more difficult for a beginner than method #1.
You can use the # action in a form action:
<?php
if(isset($_POST['SubmitButton'])){ // Check if form was submitted
$input = $_POST['inputText']; // Get input text
$message = "Success! You entered: " . $input;
}
?>
<html>
<body>
<form action="#" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Friend. Use this way, There will be no "Undefined variable message" and it will work fine.
<?php
if(isset($_POST['SubmitButton'])){
$price = $_POST["price"];
$qty = $_POST["qty"];
$message = $price*$qty;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="post">
<input type="number" name="price"> <br>
<input type="number" name="qty"><br>
<input type="submit" name="SubmitButton">
</form>
<?php echo "The Answer is" .$message; ?>
</body>
</html>
You have to use code similar to this:
echo "<div id='divwithform'>";
if(isset($_POST['submit'])) // if form was submitted (if you came here with form data)
{
echo "Success";
}
else // if form was not submitted (if you came here without form data)
{
echo "<form> ... </form>";
}
echo "</div>";
Code with if like this is typical for many pages, however this is very simplified.
Normally, you have to validate some data in first "if" (check if form fields were not empty etc).
Please visit www.thenewboston.org or phpacademy.org. There are very good PHP video tutorials, including forms.
You can see the following example for the Form action on the same page
<form action="" method="post">
<table border="1px">
<tr><td>Name: <input type="text" name="user_name" ></td></tr>
<tr><td align="right"> <input type="submit" value="submit" name="btn">
</td></tr>
</table>
</form>
<?php
if(isset($_POST['btn'])){
$name=$_POST['user_name'];
echo 'Welcome '. $name;
}
?>
simple just ignore the action attribute and use !empty (not empty) in php.
<form method="post">
<input type="name" name="name">
<input type="submit">
</form>
<?PHP
if(!empty($_POST['name']))
{
echo $_POST['name'];
}
?>
Try this... worked for me
<form action="submit.php" method="post">
<input type="text" name="input">
<input type="submit">
</form>
------ submit.php ------
<?php header("Location: ../index.php"); ?>
I know this is an old question but since it came up as the top answer on Google, it is worth an update.
You do not need to use jQuery or JavaScript to stay on the same page after form submission.
All you need to do is get PHP to return just a status code of 204 (No Content).
That tells the page to stay where it is. Of course, you will probably then want some JavaScript to empty the selected filename.
What I do is I want the page to stay after submit when there are errors...So I want the page to be reloaded :
($_SERVER["PHP_SELF"])
While I include the sript from a seperate file e.g
include_once "test.php";
I also read somewhere that
if(isset($_POST['submit']))
Is a beginners old fasion way of posting a form, and
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Should be used (Not my words, read it somewhere)

Categories