Form submit is not sending any POST data - php

On my web page, when I press the "Add Customer" link, the following happens:
the onclick handler is called, which
sets values into the forms two text fields
displays an alert (at this point you can see the new values in the text fields on the screen)
calls the form's submit button (Note: the form submits back to it's own PHP file)
The same php file is called, but there are no POST values received
I've verified this with
the code in the first line that counts the values in $_POST then displays later
using fiddler to look at the request sent to the page
I've done this type of thing numerous times before with no problems. I've copied this code to two different web servers (linux, apache) with the same result. It's probably a minor problem but I can't find it.
I've stripped out a whole bunch of code to get down to this little bit, but haven't figured out why no POST values are being sent.
You can see a working copy of this at http://www.daleann.org/dla.php. The only thing need besides the code below is /js/jquery.min.js.
Thanks for your help.
<?php
$pc=count($_POST)."<br />".date("H:i:s");
?>
<html>
<head>
<script src="/js/jquery.min.js" /></script>
<script type="text/javascript">
$(document).ready(function() {
alert("inside docReady");
$(document).on('click', "a.menuBillingOwner", function() {
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
alert("selectedBillingOwner = "+document.forms['formBillingOwner'].elements['selectedBillingOwner'].value);
document.forms['formBillingOwner'].submit();
});
});
</script>
</head>
<body>
<ul id="menuBillingOwner">
<li><a href='#' id='menuBillingOwnerAdd' class='menuBillingOwner'>Add Customer</a></li>
</ul>
<?php
$lastCustomNavSelected = $selectedBillingOwner = "";
if (count($_POST) > 0 && isset($_POST['selectedBillingOwner'])) {
$lastCustomNavSelected = "selectedBillingOwner";
$selectedBillingOwner = $_POST['selectedBillingOwner'];
}
?>
<?php echo "pc = ".$pc."<br />\n"; ?>
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
</body>
</html>

Simple, because your form fields don't have name attributes, see below
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
Add names to them, for example:
<form name="formBillingOwner" id="formBillingOwner" method="POST" action="/dla.php">
<input type="text" id="lastCustomNavSelected" name="lastCustomNavSelected" value="<?php echo $lastCustomNavSelected; ?>" />
<input type="text" id="selectedBillingOwner" name="selectedBillingOwner" value="<?php echo $selectedBillingOwner; ?>" />
</form>
Note: Probably your jQuery assignments need to be fixed too but if that was the only issue then atleast a wrong value should have been POSTed to PHP, hence that is not the issue.

Delete these 2 lines of your jQuery.
$("#selectedBillingOwner").val("11");
$("#lastCustomNavSelected").val("selectedBillingOwner");
because that will change the value of textfield before your submit the form

Related

Undefined Index when passing data to different HTML5 page

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.

How do I submit a form using image links to change form action with JS (no JQuery)?

I have a search form that needs to be processed 3 different ways:
1) The default action of the form should submit using the return/enter key.
2) If "image1" is clicked the $which_action variable needs to be updated and submit the form.
3) if "image2" is clicked the $which_action variable needs to be updated and submit the form.
(I know the $which_action does not exist in the client browser, I have just included it as a placeholder in the following code)
Here is what I currently have with some notes on what I'd like to do:
<form method="POST" action="<?php echo $which_action ?>" id="query">
<input type="text" value="<?php echo $_POST['query']; ?>" />
</form>
<image1>clicking this image should change $which_action=image1.php and POST the form value into $_POST['query'] while submitting the form.
<image2>clicking this image should change $which_action=image2.php and POST the form value into $_POST['query'] while submitting the form.
The javascript should not effect the default method of submitting the form with the enter/return key.
Thank you in advance!
<form method="POST" action="" id="query">
<input type="text" value="<?php echo $_POST['query']; ?>" />
</form>
<img src="" onclick="changeAction('image1.php')">
<img src="" onclick="changeAction('image2.php')">
<script>
function changeAction(url) {
document.getElementById("query").action = url;
document.getElementById("query").submit();
}
</script>
Do you mean something like this? http://jsfiddle.net/sebastianteres/mLBxR/
<form method="POST" action="<?php echo $which_action ?>" id="query">
<input id="the_input" type="text" value="<?php echo $_POST['query']; ?>" />
</form>
<image1 onclick="changeValue("foo")>
<image2 onclick="changeValue("bar")>
<script>
window.changeValue = function (val) {
document.getElementById("the_input").value = val;
}
</script>
Within that function you could search for the form (with an id) and change the action attribute.

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.
}

I cant transfer html data with form above 3 pages

I have 3 pages.
a1.php : add record
a2.php : do sth
a3.php : record them database
But problem is occured, transfering "html data" from a2.php to a3.php.
EXAMPLE: a record which I try to record to a1.php is that :
<form name="form1" type="post" action="sayfa_1.php">
<input type='submit' value=' gidiyoruz' >
<input type='submit' value=' gidiyoruz' >
</form>
a1.php --> a2.php --> a3. php
On a2.php, there i no problem. But then. On a3.php when I show coming data, I SEE TWO BUTTON, NO HTML CODES which is above.
pages is below.
thanks.
a1.php
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
//<![CDATA[
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
//]]>
</script>
<form name="form1" type="post" action="a2.php">
<textarea rows="20" cols="90" id="anlam" name="anlam" style="overflow:auto;" > </textarea>
<input type="submit" />
</form>
a2.php
<?php
$anlam=$_GET["anlam"];
echo $anlam;
?>
<?php echo "<form name='fm' id='fm' action='a3.php' method='get'>"; ?>
<textarea rows="2" cols="50" name="anlam" style="visibility:hidden;" /> <?php echo $anlam; ?> </textarea>
<input type="submit" />
</form>
a3.php
<?php
$anlam=$_GET["anlam"];
echo nl2br($anlam);
Pace images are above. error: a3.php (instead code, there are two button)
so you want the page to send your data from one to another when you click submit button!!
Use if condition with isset.
if(isset($_POST['name_of_submit_button']))
{
// your code of next page
}
n similarly continue with another page by giving another name to you submit button and applying the same condition!!
Try using
method="post"
for your form. You could also just use an
<input type="hidden" name="anlam" value="<?php echo $anlam; ?>"/>
to transport your variable content to the last page. Are you sure, that the hidden textarea/input contains the correct value? Check the source code in your browser for this step.

jQuery trigger click via $.post

I'm submitting data from pagex.php to pagey.php via jQuery post.
pagex.php contains
$('#btn').click(function(e) {
e.preventDefault();
var x = 'variable1';
var y = 'variable2';
$.post("/pagey.php", { var1: x, var2: y}, function(data) {});
});
pagey.php contains
<form action=....>
<input type="text" name="x" value="<?php echo $_POST['var1'] ?>" />
<input type="text" name="y" value="<?php echo $_POST['var2'] ?>" />
<input id="submit" type="submit" value="submit" />
<script type="text/javascript">$('#submit').trigger('click')</script>
So basically when i post the values from pagex.php to pagey.php, i want to automatically submit the form on pagey.php . The jQuery line at the end of pagey.php will trigger an automatic click to the submit button. However jQuery is not triggering the submit click. it works if i access paygey.php directly (i tried it with pre defined variables) but not by doing $.post from pagex. I was assuming that by using $.post from pagex, pagey should automatically get the values and run the jQuery submit. What is the problem here.
JavaScript (which powers jQuery) is not run on the server, it's run from your users browser. So from my understanding, in order to run that little bit of script you will have to actually send your users to pagey.php
<input type="text" name="x" value="<?php echo $_POST[var1] ?>" />
<input type="text" name="y" value="<?php echo $_POST[var2] ?>" />
should be
<input type="text" name="x" value="<?php echo $_POST['var1'] ?>" />
<input type="text" name="y" value="<?php echo $_POST['var2'] ?>" />
You need to fix your array indices.
you should post directly to the action url on pagey... what is the value of pagey if its a simple form that auto posts.
simple answer is to do a form post on document.ready in pagey...
I think the bigger question is why are you posting data to pagey if you just re-post it to another page using your form action?
Try posting the data directly to the action page and let us know if that works.
I bet this will be useful to some of you. Regards.
<?php
echo
"<script type='text/javascript'>
$(document).ready(function() {
$('#submit').trigger('click');
});
</script>";
?>

Categories