Data entered in index.php cannot show on process.php - php

Was trying to follow a tuitorial online on how to submit data using html to mysql database.
I created 2 php files named index.php and process.php. What i wanted to show was what I typed in inside index.php will show on process.php when I clicked on the button "add employee". But nothing showed up.
Here is what's inside index.php
<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>Add Employee</title>
</head>
<body>
<form method="post" action="">
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add Employee">
</form>
</body>
</html>
Here is a Screenshot of the form.
click here for index.php image
Wile here is what's inside the process.php
<?php
print_r($_POST);
This is what shows up
process.php image
Sorry a beginner at this. Hope you guys can help. Thank you!

Maybe you are missing the "action"?
<form method="post" action="/process.php">

You should specify what you need.Somethings like this:
<?php
print_r($_POST['first_name']);
Or use foreach loop to get full array value.
You can go through with fill up action tag so when you submit the form it will go to proccess.php page.
<form method="post" action="process.php">

Related

PHP Form Post/Redirect/Get Header Location Error

I have a simple form that I've made to help me learn php. I'm using my local host server to host the php file (form.php). However, when I refresh the page it resubmits the form, I know I can use the post/redirect/get method to negate this problem. Except implementing the header('Location: form.php'); hasn't been working, if you could take a look at the code- and tell me what I'm doing wrong, that would be much appreciated.
Code example i.e without header('Location: form.php');
<?php
if (empty($_POST) === false) {
echo '<pre>', print_r($_POST, true), '</pre>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
This is the result...
Screen Shot without header location
then I add:
header('Location: form.php');
And I get this...
To prevent a form from resubmitting on page refresh, two methods are used:
Method 1: Use AJAX + Redirect
Submit your form using AJAX and then redirect to another page using JQuery.
Method 2: Reload the page
Refresh the page using Javascript.
Your code should be like this:
<?php
if (!empty($_POST)) {
echo '<pre>', print_r($_POST, true), '</pre>';
echo '<script type="text/javascript"> location.reload();</script>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
</body>
</html>

POST method not taking file inputs

Here's my HTML:
<!DOCTYPE html>
<html>
<head>
<title>This is where we add Party.</title>
</head>
<body>
<form action="viewParties.php" method="post" enctype="multipart/form-data">
PartyList:<br>
<input type="text" name="partylist"><br>
<input type="file" name="pafile"><br><br>
President:<br>
<input type="text" name="president"><br>
<input type="file" name="pfile"><br><br>
Vice President:<br>
<input type="text" name="vicepresident"><br>
<input type="file" name="vpfile"><br><br>
Secretary:<br>
<input type="text" name="secretary"><br>
<input type="file" name="secpfile"><br><br>
<input type="submit" name="submit"><br><br>
</form>
</body>
</html>
Basically what I want to happen is for the POST Method to take both Names and File Locations. But when I print_r($_POST) in another PHP File, it only shows the Names. Does anybody know how to remedy this?
Found it. Turns out I needed, not only to print($_POST) to see what I submitted but also, to print_r($_FILES) to see what files I submitted. I did some tinkering and this is what I found. Hope this helps someone else. =)

html form with Php code not executing

I'm new to PHP. I have this html file with php in it:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Php torturail 1</title>
</head>
<body>
<p>php ahead:</p>
<?php
if (isset($_POST['submit'])){
printf('User Name: %s', $_POST['name']);
}
?>
<form method="post" action="">
<p>name:</p>
<input type="text" name="name">
<p>pass:</p>
<input type="password" name="pwd">
<p>massage:</p>
<textarea name="area"></textarea>
<p>accept:</p>
<input type="checkbox" name="chb" value="on">
<p>lucky number:</p>
<input type="radio" name="group1" value="option1">1
<input type="radio" name="group1" value="option2">2
<p>button:</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
When I open it on on a browser and click submit, the form's fileds are empty again but nothing is printed.
What is the problem?
Thank you.
2 things:
1-you must save your file as .php file.
2-fill action in your form tag
<form method="post" action="where.php">

multiple form with single submit button to retrieve value of fields within bith form

index.php
<html>
<head>
<script type="text/javascript">
function submitForms()
{
document.forms["form-1"].submit();
document.forms["form-2"].submit();
}
</script>
</head>
<body>
<form method="POST" action="form.php" id='form-1'>
<input type="text" name="txt1" />
</form>
<form method="POST" action="form.php" id='form-2'>
<input type="text" name="txt2" />
</form>
<input type="button" value="Click Me!" onclick="submitForms();" />
</body>
</html>
form.php
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Above is my code and when i submit both forms then both text-fields with their value it does not shoe me both text-field values.It only shoe me second text-field value.Please help me quickly.
I think because you try to get the params after sumbit two forms. You have sent the two forms at once and the second has stepped to the first, so the result is the return of the second form.
I think this will be better:
<html>
<head>
</head>
<body>
<form method="POST" action="form.php">
<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="submit" value="Click Me!" />
</form>
</body>
</html>
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Sorry for my english

HTML form PHP post not working

I'm writing a little website for myself and I'm having trouble with what seems like a simple line of code:
form action="send.php" method="post"
Furthermore, even a simple line like form action="http://www.google.com"> isn't working. Here is my code:
<html>
<head>
<title>
AnonMail!
</title>
</head>
<body style="font-family:'Tahoma';>
<form action="send.php" method="post">
<label for="from">From: </label><input type="text" name="from" id="from"></input></br>
<label for="to">To: </label><input type="text" name="to" id="to"></input></br>
<label for="subj">Subject: </label><input type="text" name="subj" id="subj"></input></br>
<textarea rows=10 cols=100 name="message"></textarea></br>
<input type="submit" value="Send"/>
</form>
</body>
</html>
The error starts with your body tag.
You have not closed your double quotes in the style tag of your body.
Just close it properly and then run it.
I think that is only the problem.
Here's a form that should work:
<html>
<body>
<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p><input type="submit" value="Send it!"></p>
</form>
</body>
</html>
If you're still having troubles: http://myphpform.com/php-form-not-working.php
browsers show warnings when your action refers to an external source. Some browsers do not post form at all. This is unsafe for users.

Categories