I have some PHP and HTML code which should send data from the form to my MySQL database. However, on clicking Submit in the form, the page reloads and nothing happens. No echo or anything. The HTML is in the same file as the PHP file.
PHP
<?php
if(isset($_POST['submit'])){
$usernamep = $_POST['usernameinput'];
$passwordp = $_POST['passwordinput'];
$servername = "localhost";
$username = "USERNAMECENSOR";
$password = "PASSWORDCENSOR";
$dbname = "database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO accounts (username, password)
VALUES ('$usernamep', '$passwordp')";
// use exec() because no results are returned
$conn->exec($sql);
echo "Success";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
HTML
<form method="POST" action="">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" class="button" value="Sign in">
</form>
Note: I know this code is currently subject to SQL injection, and the password is not encrypted. It is temporary starting code in an attempt to get it working first.
You lack the name attribute in the submit button, add name="submit".
<form method="POST" action="">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" name="submit" class="button" value="Sign in">
</form>
Your test is wrong. You have no input named "submit" (with attribute name = submit). It should be:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
...
}
A broad test like this will not even need a named submit. The HTML form may even be posted on another event using jQuery or JavaScript.
In your PHP file, you use isset($_POST['submit']). You don't have any form input with name="submit". You could make your submit button be
<input type="submit" name="submit" class="button" value="Sign in">
You have to include your php file into the action of the form tag.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" class="button" value="Sign in">
</form>
Related
i create form and submit button and create button=save but no send update all mysql table and db true please check my code where i can wrong?
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ob_start();
session_start();
try{
$db = new PDO('mysql:host=localhost;dbname=repair;charset=utf8','root','password');
}catch(PDOException $e){
echo 'Hata: '.$e->getMessage();
}
$menusorgu = $db -> query("select * from setting");
$menucek = $menusorgu-> fetch(PDO::FETCH_ASSOC);
?>
<form role="form" action="" method="post">
<input type="text" name="setting_title"value="<?php echo $menucek['setting_title']; ?>">
<input type="hidden" name="setting_id" value="<?php echo $menucek['setting_id']; ?>">
<input type="submit" name="save" id="save">
</form>
<?php
if(isset($_POST['save']))
{
$sql = "UPDATE setting SET setting_title = :setting_title, WHERE setting_id= :setting_id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':setting_title', $_POST['setting_title'], PDO::PARAM_STR);
$stmt->execute();
}
?>
So here is the direct link to php form: https://www.php.net/manual/en/tutorial.forms.php
You may find some interesting basic. But in your case, I will say that you should at least add a file to your action, to send your form to your controller.
As you choose the method post, I will recommand to add a hidden input to include your setting_ID:
<form role="form" action="YOURCONTROLLER.PHP" method="post">
<input type="text" name="setting_title" value="<?php echo $menucek['setting_title']; ?>">
<input type="hidden" name="setting_id" value="<?php echo $menucek['setting_id']; ?>">
<input type="submit" name="save" id="save">
</form>
I dont think it would solve all, but at least you will have a base
I am starting to learn the basics of SQL and PHP codes.
I am trying to create a simple newsletter subscription page. this would require the user to enter their email and submit. That will then get added to my database.
Now, this works well when the HTML and PHP code are separate and the submission occurs but redirects to the PHP page with the echo.
I want to get the message on the same page and I tried merging the PHP code in the page as below
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
mysql_connect("hostname", "username", "password");
mysql_select_db("db name");
$user = $_POST['email'];
$query = "INSERT INTO tablename(columname)VALUES('$email')";
echo "inserted";
}
?>
<html>
<form method="POST" action="" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
</html>
Hoever with this code it just doesnt do anything.
What have am I doing wrong here? Appreciate your expert advice.
There are few mistakes in the code, you can fix them by doing the following:
Save the file as a php file first. For example name it "email.php".
Make the form action="email.php"
Don't write two complete separate codes in the same file, one for php file and the other for html file like you did. You can include the html code inside the php code using heredoc syntax which allows you to include a long html code like the following:
echo<<<_HTMLCODE
<form method="POST" action="" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
_HTMLCODE;
In the query syntax, add $user instead $email because the variable $user contains the value submitted by the form.
Add a code to excute the inserted query. for example:
mysql_query($query);
So your final code will be like this:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
mysql_connect("hostname", "username", "password");
mysql_select_db("db name");
$user = $_POST['email'];
$query = "INSERT INTO tablename VALUES('$user')";
mysql_query($query);
echo "inserted";
}
echo<<<_HTMLCODE
<form method="POST" action="email.php" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
_HTMLCODE;
?>
I have tried the code above after I added the data of my database on the localhost and after I created a table for the emails and it worked. Here is the edited code with my database access info and the table name in my code editor:
When i opened the table emails in my database, I found the email that I had submitted using the form (your modified code):
(advice: use mysqli instead of mysql)
Please use prepare statements to prevent Sql Injections.
Here is sample code try this.
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connect = new mysqli ("localhost", "root", "usbw", "test");
if (mysqli_connect_errno()) {
echo 'Failed to connect to MySQL:' . mysqli_connect_error();
}
if (isset($_POST['submit'])) {
$email = filter_input(FILTER_VALIDATE_EMAIL, $_POST['email']);
$sql = "INSERT INTO table (email) VALUES (?)";
$stmt = $connect->prepare($sql);
$stmt->bind_param('s', $email);
$result = $stmt->execute();
if ($result) {
$msg = 'Succesfully added';
} else {
$msg = 'OOPS Error Occured';
}
}
?>
<html>
<form method="POST" action="" >
<label>Email:</label> <input name="email" type="text"/>
<input type="submit" name="submit" value="Insert" /> <br>
</form>
</html>
I want to go to other php page on pressing html button of same site. I have tried it by using header function but it was not working.
Here is the simple html code:
<input type="button" name="Insert_Ad" value="Post and ad">
If user clicks this button it will take it to another php page For Example with URL adddress 'http://localhost/Product.php/Product.php'
Here is the code of that PHP page to which I want to go on pressing button
<html>
<head>
<meta charset="UTF-8">
<title>Insert form data</title>
</head>
<body>
<form method="post" action ="Product.php" id="contact-form">
<input type="text" name="product_category" placeholder="product_category" required />
<input type="text" name="product_name" placeholder="product_name" required />
<textarea id = "address" name="product_description" placeholder="product_description" required /></textarea>
<input type="text" name="product_image1" placeholder="product_image1" required />
<input type="text" name="product_image2" placeholder="product_image2" required />
<input type="text" name="product_image3" placeholder="product_image3" required />
<input type="text" name="product_image4" placeholder="product_image4" required />
<input type="text" name="product_image5" placeholder="product_image5" required />
<div class="btn-group" role="group">
<input type="submit" class="btn btn-default" name="Add" value="Enter the box" style="margin-top: 15px; margin-right: 15px; border-radius: 4px;">
</div>
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "zz224466";
$dbname = "zain";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Add']))
{
$product_category = $_POST['product_category'];
$product_name = ($_POST['product_name']);
$product_description = ($_POST['product_description']);
$product_image1 = ($_POST['product_image1']);
$product_image2 = ($_POST['product_image2']);
$product_image3 = ($_POST['product_image3']);
$product_image4 = ($_POST['product_image4']);
$product_image5 = ($_POST['product_image5']);
$sql = "INSERT INTO zain.product (product_category,product_name,product_description,product_image1,product_image2,product_image3,product_image4,product_image5)
VALUES ('$product_category','$product_name','$product_description','$product_image1','$product_image2','$product_image3','$product_image4','$product_image5')";
mysqli_query($conn,$sql);
if (mysqli_query($conn,$sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
Kindly help me how to do that
This is the simplest way:
<input type="button" name="Insert_Ad" value="Post and ad" onclick="location.href='your_url_here'">
Or you can use a form to submit to the given url:
<form action="your_url_here">
<input type="submit" name="Insert_Ad" value="Post and ad">
</form>
I would recommend using an anchor tag. If you want to give it the appearance of a button, then use CSS to style it into a button. For instance, if you were using twitter bootstrap, this would involve giving it a class of btn
<a href="/Product.php" class='btn'>Post an ad</a>
If you insist on using a button, then you might want to wrap it around a HTML form, where the action attribute points to the page you would like to navigate to. The method has to be GET. If it is the only button in the form, then it will default to being the submit button even in the absence of a type="submit" attribute.
<form action="/Product.php" method="GET">
<input type="button" name="Insert_Ad" value="Post and ad">
</form>
I have 3 files.
1st one :
<html>
<form action="employeeDel.php" method ="post">
Enter Ssn To Delete Employee:<br>
<input type="number" name="ssnDel">
<br>
<br>
<input type="submit" value="Submit">
</form>
</html>
This form sends data to employeeDel.php.
employeeDel.php :
<html>
<form action ="employeeDelFinal.php" method="post">
<input type="hidden" name="ssn" value="ssnDel">
<?php
$ssnDel = $_POST ["ssnDel"];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "company";
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn) {
die ( "Connection failed: " . mysqli_connect_error () );
}
$sql = "SELECT * from employee WHERE ssn=".$ssnDel;
<input type="submit" name="Delete?">
</form>
</html>
From here, when user clicks on submit button, I want html form to send ssnDel value to employeeDelFinal.php file.
employeeDelFinal.php :
<?php
$ssnDel = $_POST ["ssn"];
echo ssnDel;
?>
That value never reaches here. I got an error on employeeDel.php file, it says value of ssnDel is null. I guess in the beginning of form in employeeDel file, I create ssnDel again, so it becomes null.
Is there a way to send a data from html form to employeeDel.php, from employeeDel.php to employeeDelFinal.php by using form? I tried hidden text but it didn't solve my problem as seen.
The line
<input type="hidden" name="ssn" value="ssnDel">
should be something like
<input type="hidden" name="ssn" value="<?php echo(intval($_POST['ssnDel'])); ?>">
(Assuming that ssnDel is an ID-Number.)
Otherwise that hidden variable will have the string-value ssnDel, not the value of the variable $_POST['ssnDel'].
And as already mentioned, echo ssnDel; should be echo $ssnDel; and you should use less spaces (e.g. no spaces after $_POST or function names).
There are couple of things I noticed. You have
employeeDelFinal.php :
<?php
$ssnDel = $_POST ["ssn"];
echo ssnDel;
?>
You don't have a dollar sign in your echo statement ssnDel.
And why do you have spaces in between $_POST ["ssnDel"] make it
$_POST["ssnDel"]
I have this very basic form in my html page.
<form action="post.php" method="post">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and then stores the data onto my database backend.
id also want to submit data via URL bar, such as this.
http://localhost/test.php?message=test&submit=send
but when i try to do above, nothing happens.
how can i achieve such method?
[EDIT]
my post.php
<?php
include_once("connect.php");
if (isset($_GET['submit'])) {
if ($_GET['message'] == "") {
echo " no input, return";
exit();
}
else {
$message = $_GET['message'];
mysql_query("insert into data (message) values ('$message')");
header ('location:index.php');
exit ();
}
}
else {
echo "invalid";
}
?>
use GET method instead of POST
so your code should be like follow:
<form action="post.php" method="GET">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and in the post.php you can get those Query string by using $_GET['message'] or $_REQUEST['message']
use a form GET method. to submit data of a form as a query string.
<form action="test.php" method="GET">