Seperating html form from php file - php

Previously I wrote a PHP code that includes an HTML form. So basically, the user inputs info in the HTML form and if all necessary input is filled, then the PHP file directs information to another PHP file. This is the HTML form I made This is the PHP File code (I will only put one variable of the form so that it's easier to understand:
<?php
session_start();
include('config/db_connect.php');
$Telephone = '';
$errors = array('Telephone' => '');
if(isset($_POST['submit'])){
// check Telephone
if(empty($_POST['Telephone'])){
$errors['Telephone'] = 'A Telephone number is required';
} else{
$Telephone = $_POST['Telephone'];
$_SESSION['Telephone'] = $Telephone;
if(!filter_var($Telephone, FILTER_VALIDATE_INT)){
$errors['Telephone'] = 'Telephone must be a valid Telephone number';
}
if(array_filter($errors)){
//echo 'errors in form';
} else {
// escape sql chars
header('Location: OTP.php');
}
} // end POST check
?>
<!DOCTYPE html>
<html>
<?php include('templates/header.php'); ?>
<section class="container grey-text">
<h4 class="center">Add a form</h4>
<body>
<form class="white" action="add.php" method="POST">
<label style="font-size: 16px">Your Telephone</label>
<input type="text" name="Telephone" value="<?php echo htmlspecialchars($Telephone) ?>">
<div class="red-text"><?php echo $errors['Telephone']; ?></div><p></p>
<div class="center">
<input type="submit" name="submit" value="Submit" class="btn brand z-depth-0">
</div>
</form>
</body>
</section>
<?php include('templates/footer.php'); ?>
</html>
Anyway, due to some reason, now I need to make the HTML form in a separate HTML file. So when users inputs the data in that form, the data has to be transferred to the PHP file I mentioned above and the innit['submit'] has to take place to further process it from there. In other words, previously the HTML form within the PHP file was filled and when 'submit' was clicked, the PHP file redirected it to another PHP file, but now the HTML form is in a HTML file itself and I want that when the form in that file has a click on 'submit', the PHP file is forced to follow the same procedure as it did previously. However, I cannot make the HTML file transfer the info to PHP and the PHP to directly go into the innit['submit'] function.
Here's the HTML part of the code (in the new HTML file):
<form action="work/add.php" method="POST">
<div id="Group_206">
<div id="Text_ca">
<span>電話號碼</span>
</div>
<input class="TextBox03" type="text" id="Telephone" name="Telephone">
<div id="Phone_name_position">
</div>
<div id="Group_159_m">
<input type="submit" value="Submit" >
</div>

The answer is as given in the comment by brombeer:
if(isset($_POST['submit'])){ will never be triggered, there is no
HTML element with name="submit" – brombeer

Related

Php $_Post doesn't work after certain length imput

I have a form
<form action="welcome.php" method="post" ........................">
<button id=......................ERATE NEW ....</button>
<br/><br/>
<div class="form-field">
<label for...............">....... TEST</label>
<br/>
<textarea id=...........................="false"></textarea>
<span class="clipSp............ successfully copied to clipboard" ></span>
<br/>
</div>
<br/>
<div clas..........content">
<div>ADDRESS
<br/>
<span>{{vm.displayAddress}}</span>
</div>
</div>
<button class="wButton fade" type="submit">REGISTER ACCOUNT</button>
<span class="divider-2"></span>
<button class="wButton fade" type="reset" ng-click="vm.back()">BACK</button>
.
.
.
.
Then i have this "welcome.php" file.
if(!empty($_POST['formdesiredpost'])){
$var = $_POST['formdesiredpost'];
file_put_contents("dat‌​a.txt", $var . "\n", FILE_APPEND);
header("Location: https://randomwebsiteredirect.com");
exit();
}
Its supposed to post the desired value from the form (textarea) into a txt file, data.txt and then redirect to a website.
The problem is that it only works for short length inputs. I mean aprox (15 characters) , if I input more than 15 , like 70, it simply redirects to blank welcome.php and does nothing.
Any clue of what may be happening?
On your html you don't have the "formdesiredpost" field. Please check does there exist "formdesiredpost" field.
And in your welcome.php, on the top side of your code add error reporting function:
error_reporting(0);
To see what happened after send data to welcome.php
Works fine:
index.php:
<form action="welcome.php" method="post">
<textarea name="formdesiredpost"></textarea>
<input type="submit" value="submit">
</form>
welcome.php
<?php
if(!empty($_POST['formdesiredpost'])){
$var = $_POST['formdesiredpost'];
file_put_contents("data.txt", $var . "\n", FILE_APPEND);
header("Location: https://randomwebsiteredirect.com");
exit();
}
?>
And please check your character limits of fields:
Limit number of characters allowed in form input text field

php form problems (not displaying php echo)

I've been making a website that requires a login, and I made it so that if the form is submitted, a little text will appear under the H1 tag in PHP that says Form Submitted. Right now that text isn't popping up. What did I do wrong?
Here's the code:
<?php
if ($_POST["submit"]) {
$result = "Form Submitted";
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>My email form</h1>
<?php echo $result; ?>
<form method = "post">
<div class="form-group">
<label for = "name">Your Name: </label>
<input class="form-control" type="text" name = "name" />
</div>
<div class="form-group">
<label for = "email">Your Email: </label>
<input class="form-control" type="email" name = "email" />
</div>
<div class="form-group">
<label for = "comment">Your Comment: </label>
<textarea class="form-control" name = "comment" /></textarea>
</div>
<input type="button" id = "button" name = "submit" class="btn btn-success" value="Enter"/>
</form>
</div>
</div>
</div>
</body>
</html>
As I can see on http://www.remweb.org, PHP doesn't running on your server. (http://i.stack.imgur.com/kkwCW.png)
Change your button to a submit button..
That is, replace
<input type="button" id = "button" name = "submit" class="btn btn-success" value="Enter"/>
with
<input type="submit" id = "button" name = "submit" class="btn btn-success" value="Enter"/>
Try
<?php
if (isset($_POST["submit"])) {
$result = "Form Submitted";
}
?>
Try this
<input type="submit" id ="button" name ="submit" class="btn btn-success" value="Enter"/>
<?php
$result = "";
if (isset($_POST["submit"])) {
$result .= "Form Submitted";
}
?>
Sidenote: URL pulled from comments in another answer.
"If you want, this code is live at www.remweb.org, so you can see yourself. – Aaron Janke 2 hours ago"
There are a few things wrong here.
Firstly, your domain http://www.remweb.org/ has an index file named index.html (I typed in the index.html at the end just to make sure it was the same file/form), and in doing http://www.remweb.org/index.html it showed the form. Your server seems to be using the .html file as its default index.
I also (guessed and) went to http://www.remweb.org/index.php to see if you did have an PHP index file, and it showed "hello", therefore this tells me that PHP seems to be running.
Therefore, you need to replace the .html version with .php or rename your posted code's file as "form.php" for example, and it will work.
NOTA: http://www.remweb.org/index.html (being your form) contains PHP directives that are not being parsed by your server and does not by default.
Either you do as I suggest, or instruct Apache to treat .html files as PHP.
Plus, since your form contains 3 elements and the action is used for the same page, you need to use !empty() for those in order to echo the results of those input.
I.e.
if(!empty($_POST['name'])){
$name = $_POST['name'];
echo $name;
}
and do the same for the others.
Or, seperate your .html file from PHP and use a file pointer for the action and echo everything in the other file.
I.e.: action="action_file.php"
and hold the PHP directives in that file.
Your submit button's type <input type="button" needs to be a "submit" type and not "button" <input type="submit"

PHP redirect form to URL not working

So I'm trying to use this http://www.formget.com/how-to-redirect-a-url-php-form/ as an RSVP form.
Ideally, entering the right code on (http://baby.engquist.com/invite/) will lead you to a google form. However, when I enter any code (right or wrong) and press the button, it simply refreshes back to the /invite page.
My code is as follows:
<p style="text-align: center;">
<form action="index.php" id="#form" method="post" name="#form">
<div class="row">
<div class="large-3 columns large-centered">
<div class="row collapse">
<div class="small-10 columns">
<input id="code" name="code" placeholder="Enter the code to RSVP." type="text" >
</div>
<div class="small-2 columns">
<input id='btn' name="submit" type='submit' class="button prefix" value='Go'>
</div>
</div>
</div>
</div>
<?php
include "redirect.php";
?>
</form>
</p>
And the included redirect.php:
<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$code = $_POST['code'];
if($code ='show620')
{
// To redirect form on a particular page
header("Location:http://google.com/");
} else {
print "Oops that's not the right code. Try again!";
}
?>
Thanks so much for any help!
You should have action attribute pointing to file where you do processing after submitting. In your case its redirect.php
Use :
<form action="redirect.php" > ............
And dont include redirect.php at the bottom of the form.
You need to write ob_start(); on top of your page and die(); after header("Location:http://google.com/"); in redirect.php
The php header redirect only works if it's called from a page that is completely blank. You have to change your form action to "redirect.php" and simply get rid of the code at the bottom of your html.

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)

PHP error display

I am new with php, but I have already made a registration script that works fine. But the problem is every time I press the submit button to check my error, I'm going to a new page.
My question is how I make that error comes on the same page?
The code I am useing for the html form.
I want the error display in the error div box that I made Any idea ?
<div id="RegistrationFormLayout">
<h1>Registration Page</h1>
<div id="ErrorMessage"></div>
<form action="script/registration.php" method="post">
<label for="Username">Username</label>
<input type="text" name="Regi_username">
<label for="FirstName">FirstName</label>
<input type="text" name="Regi_Firstname">
<label for="LastName">LastName</label>
<input type="text" name="Regi_Lastname">
<label for="EamilAddress">Regi_EmailAddres</label>
<input type="text" name="Regi_EmailAddres">
<label for="Password">Password</label>
<input type="password" name="Regi_password">
<button type="submit" value="Submit" class="Login_button">Login</button>
</form>
</div>
If I understand correctly, you want form validation errors there. This is a very common pattern, and the simple solution is to always set a form's action attribute to the same page that displays the form. This allows you to do the form processing before trying to display the form (if there are $_POST values). If the validation is successful, send a redirect header to the "next step" page (with header()).
The basic pattern looks like this (in very very simplified PHP)
<?php
if(count($_POST)) {
$errors = array();
$username = trim($_POST['Regi_username']);
if(empty($username)) {
$errors[] = 'username is required';
}
if(count($errors) == 0) {
header('Location: success.php');
die();
}
}
<ul class="errors">
<?php foreach($errors as $error) { ?>
<li><?php echo $error;?></li>
<?php } ?>
</ul>

Categories