Undefined Index when passing data to different HTML5 page - php

The following is one of my HTML pages.
<form id="regForm" method="post" action="enquiry_process.php" novalidate="novalidate">
<fieldset>
<legend>Personal Details</legend>
<label>First Name:</label>
<input type="text" name="owner" id="owner" /><br />
<label>Last Name:</label>
<input type="text" name="owner2" id="owner2" /><br />
</fieldset>
<p>
<input type="Submit" onclick="validateForm()"/>
<input type="Reset" value="Reset" />
</p>
</form>
The following is my 2nd HTML page.
<form id="bookForm" method="post" action="view_enquiry.php">
<?php
$fname = $_POST['owner'];
$lname = $_POST['owner2'];
?>
<input type="hidden" name="owner" value="<?php echo $fname; ?>">
<input type="hidden" name="owner2" value="<?php echo $lname; ?>">
<fieldset>
<legend>User Details</legend>
<p>Your First Name: <span id="confirm_fname"></span></p>
<p>Your Last Name: <span id="confirm_lname"></span></p>
<input type="submit" name="submit" value="Confirm Booking" />
<input type="button" value="Cancel" id="cancelButton" onclick="cancelBooking()" />
</fieldset>
The functions you see like validateForm() and cancelBooking() are Javascript functions that validate my form or return the user from the 2nd page to the 1st and I believe they have nothing to do with my question.
When I click submit on the first HTML page, it should pass on the value of the owner and owner2 to the 2nd page right?
I keep on getting Undefined index and after looking around, it seems like I have to use isset() or empty() in my PHP, but this seems to only mask my notices but does not actually fix it? When I just add isset(), it ends up giving my 3rd page Undefined Variable. The method on my forms are already post.
Is there another problem here? Thank you.
EDIT: The following is are my relevant Javascripts.
ValidateForm:
function validateForm(){
"use strict";
gErrorMsg = "";
var nameOK = chkOwnerName();
var nameOK2 = chkOwnerName2();
var isAllOK = (nameOK && nameOK2);
if(isAllOK){
isAllOK = storeBooking();
}
else{
alert(gErrorMsg);
gErrorMsg = "";
}
return isAllOK;
}
Storebooking:
function storeBooking() {
"use strict";
sessionStorage.firstname = document.getElementById("owner").value;
sessionStorage.lastname = document.getElementById("owner2").value;
window.location = "enquiry_process.php";
}
I have another function called getbooking that runs with the condition window.onload
function getBooking(){
//if sessionStorage for username is not empty
if((sessionStorage.firstname != undefined)){
//confirmation text
document.getElementById("confirm_fname").textContent = sessionStorage.firstname;
document.getElementById("confirm_lname").textContent = sessionStorage.lastname;
}
chkOwnerName and chkOwnerName2 are functions that validate the form with patterns and I don't think they're relevant.
I also updated my 2nd HTML page with Javascript related contents because I assumed it wasn't relevant at first.

You can debug with below code by adding it in your 2nd form page.
echo "<pre>"; print_r($_POST); die;
If your form data is not going to your 2nd form then Array() will come as empty.

u can try by print_r($_REQUEST[]); on second form top page (enquiry_process.php) , i hope the both the form is in same folder and name of second form page is "enquiry_process.php" .
Since u r sending data using post form u should be able to retrieve it by print_r($_POST); or print_r($_REQUEST);

"if(empty($var))" and "if(isset($var))" are conditions, they check something and execute code within "{}" if the test returns true. So they don't 'fix' problems.
Your script worked fine for me without your js. Maybe the problem.
Just try your code step by step. You will find what's wrong.

Related

Can't get form value with PHP

I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .

Form submit to another page and then review or write to a file

I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Is it possible to include a form in a wordpress page?

I have the following code in a php template called contact_us. I have created a new page which uses this template, but when you click submit it doesn't post back to the same page and display what the user entered in the form. Any ideas why this doesn't work?
Thanks,
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$comments = $_POST["comments"];
echo $name;
echo $comments;
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post" >
Name : <br/>
<input type="text" name="name" /><br/>
Comment <br/>
<textarea name="comments" name="comments"></textarea>
<br/><br/>
<input name="submit" type="submit" id="submit" value="Send" />
</form>
Make sure that you don't use "name" as a variable name. I will assume that the same thing goes for comments.
More information here
http://wpquicktips.wordpress.com/2010/02/17/use-an-empty-action-attribute-in-forms/
Does it make any difference removing <?php echo $PHP_SELF;?> from the action and leaving it blank?
I managed to get this to work by adding an id attribute to the form. I think this is something wordpress requires.

How can I execute a PHP function in a form action?

I am trying to run a function from a PHP script in the form action.
My code:
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="username()">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
I echo the form but I want the function "username" which is called from username.php to be executed. how can I do this in a simliar way to the above?
<?php
require_once ( 'username.php' );
if (isset($_POST['textfield'])) {
echo username();
return;
}
echo '
<form name="form1" method="post" action="">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
You need to run the function in the page the form is sent to.
In PHP functions will not be evaluated inside strings, because there are different rules for variables.
<?php
function name() {
return 'Mark';
}
echo 'My name is: name()'; // Output: My name is name()
echo 'My name is: '. name(); // Output: My name is Mark
The action parameter to the tag in HTML should not reference the PHP function you want to run. Action should refer to a page on the web server that will process the form input and return new HTML to the user. This can be the same location as the PHP script that outputs the form, or some people prefer to make a separate PHP file to handle actions.
The basic process is the same either way:
Generate HTML form to the user.
User fills in the form, clicks submit.
The form data is sent to the locations defined by action on the server.
The script validates the data and does something with it.
Usually a new HTML page is returned.
A simple example would be:
<?php
// $_POST is a magic PHP variable that will always contain
// any form data that was posted to this page.
// We check here to see if the textfield called 'name' had
// some data entered into it, if so we process it, if not we
// output the form.
if (isset($_POST['name'])) {
print_name($_POST['name']);
}
else {
print_form();
}
// In this function we print the name the user provided.
function print_name($name) {
// $name should be validated and checked here depending on use.
// In this case we just HTML escape it so nothing nasty should
// be able to get through:
echo 'Your name is: '. htmlentities($name);
}
// This function is called when no name was sent to us over HTTP.
function print_form() {
echo '
<form name="form1" method="post" action="">
<p><label><input type="text" name="name" id="textfield"></label></p>
<p><label><input type="submit" name="button" id="button" value="Submit"></label></p>
</form>
';
}
?>
For future information I recommend reading the PHP tutorials: http://php.net/tut.php
There is even a section about Dealing with forms.
I'm not sure I understand what you are trying to achieve as we don't have what username() is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:
<?php
require_once ( 'username.php' );
if (isset($_POST)) {
$textfield = $_POST['textfield']; // this will get you what was in the
// textfield if the form was submitted
}
?>
<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ?">
<p>Your username is: <?php echo(username()) ?></p>
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>
This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfield variable and you can display it again as you want.
I don't know if the username() function was supposed to return you the URL of where you should send the results but that's what you'd want in the action attribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.
// Edit:
If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on StackOverflow or on Google.
You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that <?php username() ?> in your HTML form.
I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.
Your form.php is called.
The form.php generates the HTML and is sent to the client's browser.
The client only sees the resulting HTML form.
When the user presses the button, you have two choices: a) let the browser do its usual thing so send the values in the form fields to the page specified in action (in this case, the URL is defined by PHP_SELF which is the current page.php). Or b) use javascript to push this data to a page without refreshing the page, this is commonly called AJAX.
A page, in your case, it could be changed to username.php, will then verify against the database if the username exists and then you want to regenerate HTML that contains the values you need, back to step 1.
I think it should be like this..
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="<?php username() ?>">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
It's better something like this...post the data to the self page and maybe do a check on user input.
<?php
require_once ( 'username.php' );
if(isset($_POST)) {
echo "form post"; // ex $_POST['textfield']
}
echo '
<form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
Is it really a function call on the action attribute? or it should be on the onSUbmit attribute, because by convention action attribute needs a page/URL.
I think you should use AJAX with that,
There are plenty PHP AJAX Frameworks to choose from
http://www.phplivex.com/example/submitting-html-forms-with-ajax
http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/
ETC.
Or the classic way
http://www.w3schools.com/php/php_ajax_php.asp
I hope these links help
You can put the username() function in another page, and send the form to that page...

Categories