php form problems (not displaying php echo) - php

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"

Related

Seperating html form from php file

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

How to send the user to different pages when the checkbox is checked?

<form entype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
Hi hello I want to ask a simple question. The code above is search.php,
I want to send the form to a different page based on if the checkbox is checked or not. If the checkbox is checked it will be directed to resqueryexp.php, but if not it will be directed to result.php
I have been trying to adding this code but it doesn't work.
<?php
if (isset($_GET['queryexp'])){
header("Location: resqueryexp.php");
}else{
header("Location: result.php");
}?>
Sorry for my bad English and Thanks in advance.
<?php
if ( isset( $_GET['submit'] )) {
if ($_GET['queryexp'] == 1 ){
header("Location: resqueryexp.php");
exit;
}
else
{
header("Location: result.php");
exit;
}
}
?>
<html>
<head><title>test</title>
</head>
<body>
<form enctype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
This code won't run here at SO, but this is how it may work on your webserver. The important part is to test if the form was submitted. So, in this case, I gave the submit button the name of "submit" and then tested with PHP to see if the form was even submitted. If the form is submitted and if the checkbox is checked, the redirect via header() occurs. Otherwise, if the checkbox is unchecked, then the redirect occurs via header to result.php. You may avoid header issues by making an adjustment to you PHP.ini settings and adding this line "output_buffering = On".
Note: usually a form with the enctype attribute having a value of "multipart/form-data" involves submitting a file and under such circumstances the method attribute should be a POST request instead of a GET; see MDN.
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
http://php.net/manual/en/function.header.php

HTML button submit not activating php code

I am having an issue with my html forms not submitting properly. I am new to forms and have been trying to get a login script going but it has been failing. I am using chrome mostly. Here is the super dumbed down code that hopefully demonstrates my issue. I am guessing what is wrong in this code is in the extended code. This is the website I used for my login coding: login account setup site mainly for reference
<?php
//this is here for testing
session_start();
//this shows
echo "something";
//none of these show
if(isset($_POST['submit'])){
echo "something";
echo "something";
echo "something";
echo "something";
}
if(isset($_POST['submit2'])){
echo "something";
echo "something";
echo "something";
echo "something";
}if(isset($_POST['submit3'])){
echo "something";
echo "something";
echo "something";
echo "something";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<footer>
<!-- I have tried it with and without an action -->
<form action="button_test.php" method="post" id="this" style="width: 100%; height: 100%; background-color: darkblue;">
<button type="submit" name="submit" id="submit" class="button" value="Submit">this is something</button>
<button type="button" name="submit2" id="submit2" class="button" value="this">this is something2</button>
<input type="button" name="submit3" id="submit3" class="button" value="true"/>
</form>
</footer>
</body>
</html>
The first button predictably reloads the page, while the others do nothing. This is the extended main code that I need working. Once again, I am guessing the code above probably explains what is wrong with the code beneath. The code beneath is primarily there to give context and more accurate answers.
<?php
//require once the DatabaseController.php file,
require_once 'Controllers/DatabaseController.php';
echo "this happend1";
// Creates a data connect object that creates a connection to
// a database
$connection = new DataConnect;
//Run the user_load function to set the user
$connection->user_load();
echo "this happend2";
/*
* Runs the in_loggedin function from a
* user object and checks if it is blank
*/
if($user->is_loggedin()!="")
{
echo "this happend3";
//return user to logged in home page if
//they are logged in
$user->redirect('index.php');
}
echo "this happend4";
/*
* THIS DOES NOT WORK FOR SOME REASON
*/
if(isset($_POST['btn-login']))
{
echo "this happend5";
//Load the variables with the new form data that has been executed
$uname = $_POST['txt_uname_email'];
$umail = $_POST['txt_uname_email'];
$upass = $_POST['txt_password'];
//If the user->login call is successful, go to home fully logged in
if($user->login($uname,$umail,$upass))
{
//Go to home, fully logged in
$user->redirect('index.php');
}
else
{
//Error
$error = "Wrong Details !";
}
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title> Josiah</title>
<link href="Assets/Stylesheets/styles-home.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
include_once 'header.php';
?>
<!-- recent blog post: contains the most recent blog post-->
<div id="main-content" class="content">
<div id="main-content-inner" class="inner-content">
<!-- this is the container for the form -->
<div class="form-container">
<!-- create a form that uses the "post" method which allows php to do $_POST on
corresponding variable names -->
<form method="post">
<h2>Sign in.</h2><hr />
<!-- checks if there was an error. If the error variable is set -->
<?php
if(isset($error))
{
?>
<!-- displays the error -->
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?> !
</div>
<?php
}
?>
<!-- this is a form-group -->
<div class="form-group">
<!-- this is a text input. -->
<input type="text" class="form-control" name="txt_uname_email" placeholder="Username or E mail ID" required />
</div>
<!-- this is a form-group -->
<div class="form-group">
<!-- this is a text input. -->
<input type="password" class="form-control" name="txt_password" placeholder="Your Password" required />
</div>
<!-- splits parts of the form -->
<div class="clearfix"></div><hr />
<!-- this is a form group holdng the submit button -->
<div class="form-group">
<!-- the button interestingly has multiple classes -->
<button type="submit" name="btn-login" class="btn btn-block btn-primary">
<!-- splits parts of the form. Dont know specifically what this does. -->
<i class="glyphicon glyphicon-log-in"></i> SIGN IN
</button>
</div>
<br />
<label>Don't have account yet ! Sign Up</label>
</form>
</div>
</div>
</div>
<!-- footer: contains social media, site map, and an up arrow-->
<?php
include 'footer.php'
?>
</body>
</html>
Since php://input shows the form value (according to the test I had you run in comments), we know that PHP is properly receiving the data.
By default, PHP will place request parameters in $_GET and $_POST as appropriate. In your case this doesn't seem to be happening. I suggest you look at your php.ini file and add the setting below:
variables_order = "GPCS"
There may already be a variables_order line, so just edit it in that case (documentation).
So after some searching I forgot to disclose that I was using PHPstorm. The reason why this did not occur to me is that PHPstorm is not installed on another computer that I have been using, however since I use Intellij on both computers, this bug in PHPstorm transferred regardless since Intellij will use PHPstorm plugins. It seems that 10.1-10.3 this bug persists where POST remains empty. I am not sure if it was fixed so I moved over to Apache and have it doing the web serving instead. Everything works, or at least works better. The 2 solutions are either wait for PHPstorm to fix this bug, or better move over to Apache.
link to site post

Weird PHP within html

I've written some html code, and once i tried to place some PHP inside it, anything below this sign ?> wouldn't appear!!! I have some pictures and text that wouldn't appear unless I place it above. I'm writing with Bootstrap 2.3 and phpMyAdmin 4.10. all languages. Thank you for your time in advance.
here is my code so far:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="bootstrap/css/myStyle.css">
<title>OJRA - Registration</title>
</head>
<body>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username</h6>
<input type="text" name="username" placeholder="Your Username">
<h6>Password</h6>
<input type="password" name="usrPassowrd" placeholder="Your password">
<h6>Email</h6>
<input type="email" name="usrEmail" placeholder="Your Email"><br>
<input class = "btn btn-default" type="submit" value = "Register">
</form>
</div>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username:</h6> <input type="text" name="username" placeholder="Your username">
<h6>Password:</h6> <input type="password" name="usrPassowrd" placeholder="Your password">
<input class = "btn btn-default" type="submit" value="Sign in">
</form>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$username = $_POST['username'];
if($username == "")
{
die("cannot go empty");
header("location:index.php");
exit;
}
$password = $_POST['usrPassword'];
$email = $_POST['usrEmail'];
$query = "insert into tz_members values('$username', '$password', '$email')";
mysql_query($query) or die(mysql_error());
?>
</div>
<img src="sexymotivation.jpg" style="margin-top:-800px; margin-right:10px;" class="pull-right">
</body>
</html>
A few things have already been outlined (as answers) that do make sense, however I spotted a few typos in your inputs that will prevent your form from working, plus a few other points.
Here are a few of my recommendations:
First, this (in 2 instances) has a typo in it name="usrPassowrd" which should read as name="usrPassword" to go with your existing $password = $_POST['usrPassword'];
As I stated in my original comments:
Comment #1: die("cannot go empty"); header("location:index.php"); exit; Your header won't do anything, because it DIE()'d and that will cease to go any further.
Comment #2: What I suspect is going on is, because you've got your entire code inside one big clump, and that if certain conditions aren't met... it still wants to keep going. Now, I suggest that you put a conditional statement wrapped around your PHP....
such as if(isset($_POST['submit'])){ // PHP } then add this to your submit button name="submit" --- I also suggest you split up your form and your PHP/SQL altogether and make it submit to another page instead, with the same conditional statement I've already outlined.
If you absolutely want to execute everything in one page, try the following:
Note: I borrowed the img src from user3009875's answer also.
(Rewrite)
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="bootstrap/css/myStyle.css">
<title>OJRA - Registration</title>
</head>
<body>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username</h6>
<input type="text" name="username" placeholder="Your Username">
<h6>Password</h6>
<input type="password" name="usrPassword" placeholder="Your password">
<h6>Email</h6>
<input type="email" name="usrEmail" placeholder="Your Email"><br>
<input class = "btn btn-default" type="submit" value = "Register">
</form>
</div>
<div style="width:400px; height:400px; border-radius:5px; background-color:#aaa; margin-left:100px;">
<form action="" method="post" style="margin-left:20px;">
<h6>Username:</h6> <input type="text" name="username" placeholder="Your username">
<h6>Password:</h6> <input type="password" name="usrPassword" placeholder="Your password">
<input class = "btn btn-default" name="submit" type="submit" value="Sign in">
</form>
<?php
// this below, will prevent a premature execution of code
if(isset($_POST['submit'])){
define('INCLUDE_CHECK',true);
require 'connect.php';
$username = $_POST['username'];
if($username == "")
{
die("cannot go empty");
// header("location:index.php"); // commented out
exit;
}
$password = $_POST['usrPassword'];
$email = $_POST['usrEmail'];
$query = "insert into tz_members values('$username', '$password', '$email')";
mysql_query($query) or die(mysql_error());
} // end brace for if(isset($_POST['submit']))
?>
</div>
<!-- commented out original img src -->
<!--
<img src="sexymotivation.jpg" style="margin-top:-800px; margin-right:10px;" class="pull-right">
-->
<!-- new img src by user3009875 in an answer given -->
<img src="sexymotivation.jpg" style="margin-top:100px; margin-right:10px;" class="pull-right">
</body>
</html>
You can't use header() there to perform a redirection because you've already outputted some HTML and PHP has flushed HTTP headers.
The reason that <img> disappeared is probably that you called die(). This function terminates the whole page at once.
If you see cannot go empty, you should check the form to make sure you posted username field. If you see some error message about MYSQL, it is mysql_query($query) that fails.
By the way, your code has a SQL Injection problem.
IT doesn't load below ?> because script fails. This is common behaviour when there is a bug.
Check tail -f /var/log/apache2/error.log on linux (terminal)
Check tail -f /var/log/apache2/error_log on max (terminal)
windows -> I have no idea... somewhere in C:\\
At this point:
die("cannot go empty");
you stop the script. The following PHP code will not be executed and the HTML will not be sent to the user.
It's possible that there is an error when executing your PHP code, that would prevent it from going through the rest of the file.
Using your browser's Developer Tools, check the contents of the HTML that was returned, it's possible that the last line could be an error message. If you have erorr_reporting off then it would write to your error log only.
/var/log/apache2/error.log is a common location for the error log file if you are using Apache on a Linux machine.
As a side note, that code you have is very dangerous, do not use data sent from the client directly in a SQL statement, you need to sanitize otherwise you make your web app vulnerable to SQL injection.
Consider using a prepared statement
http://php.net/manual/en/pdo.prepare.php
Try:
<img src="sexymotivation.jpg" style="margin-top:100px; margin-right:10px;" class="pull-right">
I think setting the top margin to -800px will cause it to disappear from the screen.
Also, make sure your image is of .jpg.

Hide form with php on submit?

I have a simple php - password protecton / login based on a form which works. PHP-code is in page "secure.php" and includes the html-file "accessed.html" when user + pass is correct.
But i want the form to hide when hidden page (accessed.html) is shown.
I have tried wrapping the form in a div and using javascript and display: none to hide, but it doesnt work - not locally or on server.
What am i doing wrong? It doesnt have to be js hiding the form after login..
PHP in top
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "a"
&& $pass == "a")
{
include("accessed.html");
echo "<script>
document.getElementById('wrap').style.display = 'none';
</script>";
}
if(isset($_POST))
?>
And the form in the body:
<div id="wrap">
<form method="POST" action="secure.php">
User <input type="text" name="user"></input>
Pass <input type="text" name="pass"></input>
<input type="submit" name="submit" value="access page"></input>
</form>
</div>
Move the <script> to after wherever you display "#wrap"
If you want to do it on the server side (PHP) just use an if statement.
I think this is what you are looking for my friend
<html>
<body>
<div id="wrap">
<form method="POST" action="temp.php" target = "hidden">
<input type="text" name="user">User</input>
<input type="text" name="pass">Pass</input>
<input type="submit" name="submit" value="access page" onclick = "closediv()"></input>
</form>
</div>
<iframe name = "hidden" id = "hidden" style = "display: none"></iframe>
<script>
function closediv(){
document.getElementById("wrap").style.display = "none";
}
</script>
</body>
</html>
With the above code, you'll need to do your PHP processing on the current page. If this isn't doing what you want it to do and I misread your question, let me know and we can work on your predicament.

Categories