Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need help with creating a form. It can be on blank page, I just need forms on a page.
example : NAME _______
And I would like the filled form be stored on the server so I can print them.
Print example: NAME firstname lastname
Thanks in advance!
Let's say we have two files. The first file, called index.html is a pure-HTML page that displays a form. The second file is called process.php which handles the data from the form server-side.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Example Form</title>
</head>
<body>
<form action="process.php" method="POST">
<label for="firstname_input">First Name:</label><br>
<input type="text" id="firstname_input" name="firstname" /><br>
<label for="lastname_input">Last Name:</label><br>
<input type="text" id="lastname_input" name="lastname" /><br>
</form>
<input type="submit" value="Submit" />
</body>
</html>
process.php:
<?php
if (!isset($_POST['firstname']) || !isset($_POST['lastname']))
{
die("Error! Both the firstname and lastname must be specified.")
}
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
echo sprintf("You specified a first name of '%s' and a last name of '%s'.", htmlspecialchars($firstName), htmlspecialchars($lastName));
From here, you can store $firstName and $lastName in a file or a database (such as MySQL) to store them permanently, but that seems to be beyond the scope of the question. If you choose to store them in MySQL, something like this should work:
$sql = $db->prepare("INSERT INTO user (first_name, last_name) VALUES (:first_name, :last_name)");
$sql->bindValue(':first_name', $firstName);
$sql->bindValue(':last_name', $lastName);
if (!$sql->execute())
{
die("Failed to add user to database.");
}
You didn't specify how or where you wanted to store the data, and there are many other ways to store it, but this is how you'd do it in MySQL.
Here is the code for it :
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
For getting that values in next page u can use ,
action = POST
this parameter in form tag.
Related
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 1 year ago.
I am trying to insert data into a database from HTML form using php. I made two files html form and other is PHP script. When I click on submit in html form, it shows me the php code. I am using wamp server for database. I put my html files in C:/wamp64/www directory and html files at my local directory. The database table is :
id int(11)
fname varchar(30)
salary int(11) . Id is not auto-incremented and it is a primary key.
Html code:
<html>
<body>
<h2>Employee's Information</h2>
<form action="employee.php" method="POST">
<label for="id">Enter employee id:</label><br>
<input type="text" id="id" name="id" value=""><br>
<label for="fname">Enter First name:</label><br>
<input type="text" id="fname" name="fname" value=""><br><br>
<label for="salary">Enter Employee Salary:</label><br>
<input type="text" id="salary" name="salary" value=""><br><br>
<input type="submit" id="submit" name="submit" value="Submit">
</form>
</body>
</html>
Php code:
<?php
$mysql_hostname="localhost";
$mysql_username="root";
$mysql_password="";
$mysql_database="employee";
$con=mysql_connect($mysql_hostname,$mysql_username,$mysql_password);
if(!$con){
die('Connection Error: '.mysql_error());
}
mysql_select_db($mysql_database, $con);
if(isset($_POST['submit']))
{
$s_id = $_POST['id'];
$s_name = $_POST['fname'];
$salary = $_POST['salary'];
$employeeinsert = "INSERT INTO employee1
(id, fname, salary)
VALUES('".$s_id."','".$s_name."','".$salary."')";
if(!mysql_query($employeeinsert,$con)) {
echo "Error: " .mysql_error($con);
} else {
echo "1 record added";
}
}
?>
The code is neither giving any error on submitting data nor it is inserting the data into the database.
I am not getting what the error is.
If this is false then the code successfully produces no output:
if(isset($_POST['submit']))
Which is what's happening, since the condition is false. The form has a submit button, but that button has no name attribute to its value isn't sent to the server:
<input type="submit" value="Submit">
Give it a name:
<input type="submit" name="submit" value="Submit">
It's always a good idea to have some kind of indication of any given code branch, even if just logging something somewhere so you can see what's happening. Code will happily produce no output/result if that's what it's instructed to do, but as you've discovered it can leave you with no information about what's happened.
As an aside, and this is important, your code is wide open to SQL injection. You'll want to start addressing that.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Helou,
I have a form like this
<form id="some" data-request="checkout" class="checkoutForm">
<div class="form-group mb10">
<label for="firstName" class="col-sm-12">Name <span class="color-main">*</span></label>
<input class="form-control checkout-form-border" id="firstName" value="" type="text">
</div>
<div class="form-group mb10">
<label for="Address" class="col-sm-12">Address <span class="color-main">*</span></label>
<input class="form-control checkout-form-border" id="address" value="" type="text">
</div>
etc etc ....
</form>
Now, I want on submit button show new page with collected data from this form in a table body.
Can I do this via php and jquery or new custom component?
Thanx
PHP:
need a submit button in the form
need action=" " and method="post" attributes added to your form tag
need to add name attributes to your input boxes
Top of page,
if(isset($_POST['submit'])){
$variable = $_POST['namedOfInputBox'];
echo "<table><tr><td>$variable</td></tr></table>";
{
What that says is if the form is submitted (submit being name of your submit button, generally name it submit), the values coming through are these and in PHP we "echo", or print, out html code and place our variable here. You can either use :
echo "<table><tr><td>$variable</td></tr></table>";
or
echo "<table><tr><td>" . $variable . "</td></tr></table>";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to add some data to mysql table from another mysql table and at the same time i want to add some form $_POST values to the same table in the same row.
Note : IM NEW TO PHP AND MYSQLI
Basically im looking for the correct syntax, please do help me and thanks in advance.
<?php
if(isset($_POST['ask'])) {
require('includes/connection.php');
$problem = $_POST['problem'];
$expecting = $_POST['expecting'];
//SELECT * FROM users WHERE email = '$email' AND
$string = "INSERT INTO ask(users_id,username,email,age,sex,problem,expecting) SELECT id,username,email,age,sex FROM users AND VALUES('$problem','$expecting') WHERE users.email = '$_SESSION[email]'";
$query = mysqli_query($dbc,$string);
if($query) {
echo 'yes';
} else {
echo 'something is wrong' . $query . mysqli_error();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="ask">
<h1>Ask</h1>
<form method="post" action="logged.php">
<p><label>Problem: </label><input type="text" name="problem" value="<?php if(isset($problem)) { echo $problem; } ?>"></p>
<p><label>Expecting: </label><input type="text" name="expecting" value="<?php if(isset($expecting)){ echo $expecting; } ?>"></p>
<p><input type="submit" name="ask" value="Ask"></p>
</form>
</div>
<form method="post" action="logged.php">
<input type="submit" name="logout" value="Logout">
</form>
</body>
</html>
Your first problem is wrong database design. Instead of duplicating user's data in the questions table, you have only to link it. Read up on database normalization, and change your ask table by removing username,email,age,sex fields.
Then get user_id from users table with SELECT query.
Then run a conventional INSERT query
INSERT INTO ask(users_id,problem,expecting)
Note that you have to use prepared statements for database interactions.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
From the HTML page I am submitting some records to a PHP page, where the records will be saved to the DB and then show a
message saved successfully
message to the user on the same HTML page.
When i submit my form, the records are sent to the HTML page and it displays the success message on another page and not on the same HTML page (where the form code is written). How can i correct it?
HTML
<form action="save.php" class="tsc_form_contact_dark nolabel" method="POST">
<input type="text" name="name" class="form-input" placeholder="Name (required)" required />
<input type="email" name="email" class="form-input" placeholder="Email (required)" required />
<input class="form-btn" type="submit" value="Send Message" />
</form>
PHP
<?php
$name = $_POST["name"];
$email = $_POST["email"];
// Do other stuff
echo "Success";
?>
All you need to do is set a message variable. Something like this:
$msg=isset($_GET['msg']) ? $_GET['msg'] : "";
<div><?php echo $msg; ?></div>
<form action="save.php" class="tsc_form_contact_dark nolabel" method="POST">
//rest of the form
And in the php, you need to redirect to the form page something likt this:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
// Do other stuff
$msg = "Success";
$redirecturl = "form_page.php?msg=".$msg;
header("Location: $redirecturl");
?>
There could be other methods to send result message using session, but I would recommend not doing that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$redirect=$_GET["r"];
if ( isset($_GET["r"]) ){
header("location: http://" .$redirect);
} else {
$redirect = "mickiewiki.nl/login/profile.php";
header("location: http://" .$redirect);
this code won't work? I want it to be like if I go to mickiewiki.nl/login/login.php?r=mickiewiki.nl/Doneer.php that when I login I go back to the page Doneer.php
When I login, I am always being sent to profile.php no matter if I add my r or not
<?php
//allow sessions to be passed so we can see if the user is logged in
session_start();
//connect to the database so we can check, edit, or insert data to our users table
$database = mysql_select_db('***', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "login/functions.php";
$selectedid = $_GET['id'];
if(empty($selectedid)) {
$selectedid = $_SESSION['uid'];
}
if(strcmp($_SESSION['uid'],"") == 0){
header("location: login/login.php?r=mickiewiki.nl/Doneer.php");
} else {
?>
In your form on your website, you are not setting r through GET. You have username, password & submit for your form - and nothing for GET. If you are submitting r with your form (which I cannot see that you are), then you should probably be checking for $_POST['r'] instead of $_GET['r'].
Basically, you need to add a hidden field for your r variable and submit that along with your form.
<form method="post" action="login.php">
<input type="hidden" name="r" value="<?php print $_GET['r']; ?>">
<p><label for="name">Username: </label>
<input type="text" name="username" /></p>
<p><label for="pwd">Password: </label>
<input type="password" name="password" /></p>
<p>
<input type="submit" id="submit" value="Login" name="submit" />
</form>
but, you must do a control for $_GET["r"] ... and why it doesn't work?