Unable to connect php and html file - php

i have added action value
Registration file
<body>
<div id="registration">
<h2><b><i>Electronic Montessori Learning</i><b></h2>
<form id="RegisterUserForm" action="connect.php" method="post">
<fieldset>
<p>
<label for="name">Name</label>
<input id="name" name="name" type="text" class="text" value="" />
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" class="text" type="password" />
</p>
<p>
<button id="registerNew" name="registerNew" type="submit">Register</button>
</p>
</fieldset>
</form>
</div>
<body>
I have not posted all code of registration.html as it is working fine
connect.php
<?php
$db=mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("users") or die(mysql_error());
echo "Connected to Database";
$name ='';
$password ='';
if(isset($_POST['registerNew'])){
// Storing form values into PHP variables
$name = $_POST['name']; // Since method=”post” in the form
$password = $_POST['password'];
}
mysql_query("INSERT INTO user_eml(Name, Password) VALUES('$name', '$password' ) ")
or die(mysql_error());
echo "Data Inserted!";
echo 'Thank you for submitting your details!';
?>
but still its not working and when i press register button it shows php code of connect.php

Your form doesn't know where to send its data to:
<form id="RegisterUserForm" action="" method="post">
should be
<form id="RegisterUserForm" action="connect.php" method="post">

If on clicking the Register button , php code appears on the page , then most probably it has nothing to do with your code. Please check if you are directly double-clicking the html file for your testing .
Please execute it by entering the localhost page address in the browser instead.
Eg : http://localhost/registration.html
Now the HTML should be able to execute the PHP code.
-ShazzDecoder

You have to add connect.php as your action attribute value in your form :
<form id="RegisterUserForm" action="connect.php" method="post">

With action="" in your form, nothing gets submitted. You need to tell it to call the connect.php

if you use the same page as action , you should copy the code in action.php on the page that contains form , or replace action="" with action="connect.php"

If on clicking the Register button , php code appears on the page , then most probably it has nothing to do with your code. Please check if you are directly double-clicking the html file for your testing . Please execute it by entering the localhost page address in the browser instead.
Eg : http://localhost/registration.html
Now the HTML should be able to execute the PHP code.
And your folder containing html and php file should be in XAMPP->htdocs.

Related

Check if the form is submited does not work

I want to echo something once a form is submitted. But when I click the submit button, it seems that the page is just refreshing itself and I do not see the word that I have written in the echo section. Here is my code:
<?php
if (isset($_POST['submit'])) {
echo "submitted";
}
?>
<h3>Post your form here</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Insert a title here</label><br>
<input name="title" type="text" placeholder="add a title"><br><br>
<label>Insert the body here</label><br>
<textarea name="body" placeholder="insert the body here "></textarea><br><br>
<input type="submit" value="submit" name="submit"><br>
</form>
I also tried the code by removing the isset function, but that did not work, either.
In form action, you have missed an echo.
action="<?php echo $_SERVER['PHP_SELF']; ?>"
This is working code. I test it on my Machine.
Why you don't use else statement to test it better.
here is the code.
Note (Check your localhost server settings)
<?php
if (isset($_POST['submit'])) {
echo "submitted";
}
else
{
echo "Not working";
}
?>
<h3>Post your form here</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Insert a title here</label><br>
<input name="title" type="text" placeholder="add a title"><br><br>
<label>Insert the body here</label><br>
<textarea name="body" placeholder="insert the body here "></textarea><br>
<br>
<input type="submit" value="submit" name="submit"><br>
</form>
Actually the credit goes to #ashok. He was right that I needed to check localhost server settings.
I am using Phpstorm to write PHP codes. Whenever I click on the Chrome browser to see the results, it takes me to
http://localhost:63342/ name of the file page.
The port 63342 is the default port used by Phpstorm. Since I am using Xampp and it runs on port 8080. I changed port number 63342 to 8080, and it worked.
If you are submitting to the same page, you really dont need to define an action.
Instead of isset, use !empty as such
if(!empty($_POST['submit'])){
echo "success";
}
I must bring to your attention that if the suggestions don't work, you should start with debugging what you are actually receiving from the form POST, using:
<pre>
<?php var_dump($_POST); ?>
</pre>
Turning on error reporting in your php settings is also a good start for debugging.

mysql_query("INSERT INTO... is not working

I'm trying to create a registration page. The page is successfully connected to phpMyAdmin database but it does not echo anything when i click the register button.
<html>
<head>
</head>
<body>
<?php
INCLUDE "connect.php";
INCLUDE "functions.php";
INCLUDE "titlebar.php";
?>
<div id="loginform">
<h1>Register</h1>
<form name="Register" action="register.php" method="post">
<?php
if(isset($POST["submit"])){
$username = $_POST["username"];
$password = md5 ($_POST["password"]);
if(empty($username) or empty($password)){
echo "<p>Fields Empty!</p>";
} else {
mysql_query("INSERT INTO login VALUES('',$username','$password','2','')");
echo "<p>Successfully Registered!</p>";
}
}
?>
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username"/></p><p>
<label for="password">Password: </label>
<input type="password" name="password" id="password"/></p><p>
<input type="submit" name="submit" value="Register" />
</p>
</form>
</div>
</body>
The problem is with the post method.
use $_POST instead of $POST
You have mysql error
Not: $username'
but '$username'
And next time display mysql errors with mysql_error().
At the beginning, I am not sure what isset($_POST['submit'] should return, but as already mentioned in the comments you missed a single quote.
Additionaly i would use:
$password = password_hash($_POST['password'],
md5 is deprecated and thus not safe. If you write a login script you can use password_verify(plainPW, hashPW)
You also need to specify a database and login into it. I recommend to look at the W3 Schools examples they are very in-depth and have good examples.
W3 school mysqli page
also write a die() at the end of your script and do not foregt to close the connection.

How can I write the data entered in a form to a text file on button click?

I'll preface this with I'm not a coder nor aspiring to become one.
I just want to play around with something simple.
Please don't feel bad about spoon-feeding me here haha.
All I want is when I hit a my submit button the text entered in the text field is saved to a file called log.text
I want it to overwrite each time.
Once data has been written I want it to redirect to another page.
Tried this but it doesn't create the file nor write to it even if I create it manually. The redirect also doesn't work because I'm an idiot.
Any help guys? :(
<?php
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
?>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
It is because the action element of the form is empty.
It should be \n
<form action="action.php(or any other php file that is handling the form)" method="post" name="form">
This guide offered me the solution I was after.
Thanks anyway guys!
http://www.howtoplaza.com/save-web-form-data-text-file
Because you dint checked whether the form is submitted or not. if submited create log. code given below
<?php
if(isset($_REQUEST['submit']))// if to check whether submit name is passed or not
{
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
}
?>
<html>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
</html>

What is the correct/right code for uploading or inserting values from input html tag into mysql using php?

What is the correct/right code for uploading or inserting values from input html tag into mysql using php?
I have php file and html file, but when I try to click the submit button, it only display the php file. My phpMyAdmin connected to my web host, while the html page is not
Html code:
<form action="add.php" method="post" enctype="multipart/form-data">
<input type="text" name="contract_num"><br>
<input type="text" name="random_file"><br>
<input type="submit" value="Upload file">
</form>
PHP:
<?
$contract_num=$_POST['contract_num'];
$random_file=$_POST['random_file'];
mysql_connect("your.hostaddress.com", "username", "password")
or die(mysql_error());
mysql_select_db("contract")
or die(mysql_error());
mysql_query("INSERT INTO `contract` VALUES ('$contract_num', '$random_file')");
Print "Your information has been successfully added to the database.";
?>
I think you mean "What is the correct/right code for this one?"
Please call first the html page or including the php file.
Created a page add.php, which contains this.
<?
$contract_num=$_POST['contract_num'];
$random_file=$_POST['random_file'];
mysql_connect("your.hostaddress.com", "username", "password")
or die(mysql_error());
mysql_select_db("contract")
or die(mysql_error());
mysql_query("INSERT INTO `contract` VALUES ('$contract_num', '$random_file')");
Print "Your information has been successfully added to the database.";
?>
Then a page form.php, which includes the add.php
<?php
include "add.php";
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="contract_num"><br>
<input type="text" name="random_file"><br>
<input type="submit" value="Upload file">
</form>
You can call first the form.php in your browser.
When you submit your form the post request is send to your php file. That is when you submit your form all the data filled inside the form will send to post your php file and it is processed there. If you want to see the html file again try to redirect to html file like
header("Location: ".$_SERVER['HTTP_REFERER']);
You can send them via javascript, call a js function on 'submit' click , read all input and redirect to 'add.php' page with parameters ..

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