473,379 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Cannot pass form variable(s) to PHP

Hi to all,

I have made a script that uses both JavaScript and PHP. The script has a
form and when it is submitted, a JavaScript confirmation box appears, asking
the user to confirm their action. If they click No, the action is cancelled
and if they click Yes, the form is submitted.

If only it were that simple... The problem is that when the form is
submitted using form.submit() within a JavaScript function, no form
variables are being sent through the POST method, however, if I remove all
JavaScript and then submit the form, everything works fine.

The complications are that I need the confirmation boxes and I need to use a
form, not just a hyperlink, because I need to pass the contents of a text
field through the POST method too...

Any ideas?
Jul 23 '05 #1
7 1871
NetCoder wrote:
If only it were that simple... The problem is that when the form is
submitted using form.submit()


Avoid submitting forms using JavaScript. If at all possible, use a regular
submit button and perform any tests using the onsubmit event.

<script type="text/javascript">

function myFunction() {
if (condition1) {
return true;
} else {
return false;
}
}
</script>

<form action="foo" onsubmit="return myFunction()">
<input type="submit">
</form>
--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #2
David Dorward wrote on 13 jun 2004 in comp.lang.javascript:
if (condition1) {
return true;
} else {
return false;
}


return condition1;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #3

"David Dorward" <do*****@yahoo.com> wrote in message
news:ca*******************@news.demon.co.uk...
NetCoder wrote:
If only it were that simple... The problem is that when the form is
submitted using form.submit()


Avoid submitting forms using JavaScript. If at all possible, use a regular
submit button and perform any tests using the onsubmit event.

<script type="text/javascript">

function myFunction() {
if (condition1) {
return true;
} else {
return false;
}
}
</script>

<form action="foo" onsubmit="return myFunction()">
<input type="submit">
</form>
--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is


The above suggestion works, however, whichever button I click now, Yes or
No, the form submits anyway...

Here's the code I'm using:

*** form.html ***

<html>
<head>
<title>New Record</title>
<script language="JavaScript">
function confirm_action() {
confirm_text = confirm("Are You Sure?");
if (confirm_text) {
alert("Action Confirmed. Click OK To Continue...");
}
else {
alert("Action Cancelled.");
return false;
}
}
</script>
</head>
<body>
<form method="POST" action="process.php" onSubmit="confirm_action();">
<p><input type="text" name="product_code" size="8">&nbsp;<input
type="submit" name="new_record" value="Insert Information"></p>
</form>
</body>
</html>

*** process.php ***

<?php
$new_record = $_POST["new_record"];
if ($new_record) {
$product_code = $_POST["product_code"];
if (!$product_code) {
echo "<p>No product code was specified. Please go back and type in a product
code.</p>";
}
else {
// Connect to MySQL server and insert product code into database.
header("Location: form.html");
}
}
else {
echo "<p>This file cannot be accessed directly.</p>";
}
?>

The above code, submit's the form whatever the user selects...
Jul 23 '05 #4
NetCoder wrote:
<form action="foo" onsubmit="return myFunction()">
The above suggestion works, however, whichever button I click now, Yes or
No, the form submits anyway... <form method="POST" action="process.php" onSubmit="confirm_action();">


There is a pretty significant difference between the two code snippits. You
lost the "return" keyword somewhere along the line.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #5
NetCoder wrote:
<?php
$new_record = $_POST["new_record"];
if ($new_record) {
$product_code = $_POST["product_code"];
if (!$product_code) {
echo "<p>No product code was specified. Please go back and type in a product
code.</p>";
}

I know this is an example however wouldn't the above check bet better
done in the JavaScript code? I mean why make a trip to the server if you
can easily tell in the JavaScript that the field was not specified?
--
If you drink, don't park. Accidents cause people.
Jul 23 '05 #6
Lee
Andrew DeFaria said:

NetCoder wrote:
<?php
$new_record = $_POST["new_record"];
if ($new_record) {
$product_code = $_POST["product_code"];
if (!$product_code) {
echo "<p>No product code was specified. Please go back and type in a product
code.</p>";
}

I know this is an example however wouldn't the above check bet better
done in the JavaScript code? I mean why make a trip to the server if you
can easily tell in the JavaScript that the field was not specified?


It should be checked both places.

Checking on the client side should only be for the visitor's
convenience. Anything that you care about needs to be checked
again on the server, because some visitors may have Javascript
disabled (and because it's easy to bypass client-side checks
even without disabling Javascript).

Jul 23 '05 #7

"David Dorward" <do*****@yahoo.com> wrote in message
news:ca*******************@news.demon.co.uk...
NetCoder wrote:
<form action="foo" onsubmit="return myFunction()">
The above suggestion works, however, whichever button I click now, Yes
or No, the form submits anyway...

<form method="POST" action="process.php" onSubmit="confirm_action();">


There is a pretty significant difference between the two code snippits.

You lost the "return" keyword somewhere along the line.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is


Sorry about that, the eyes managed to miss that part! Well, tried it out and
it works brilliantly (the way it should do!) so thanks very much!

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same...
2
by: Cory | last post by:
I'm using ASP to run a batch file. I need to know how to pass a variable to this .bat file. Is this possible? I'm using the following code but need to know how to pass ASP variable. set...
1
by: Orest Kinasevych | last post by:
Okay, I made sense of the earlier suggestions and realized I was on the right track -- I appreciate the feedback which got me to this point. The suggestions posted here indeed worked and...
8
by: Vijay | last post by:
Hi all, Im using gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20) on 64bit linux server im trying to compile following code --------------------sam.cpp--------------------- #include...
2
by: bebelino | last post by:
Hello, this should be an easy one, but I've had always troubles with it. How to pass trough a querydef-variable, form-variable and so on from a function to the caller-routine? Is there a simply...
12
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
2
by: John Kelsey | last post by:
I am an old, longtime C programmer surprised and confused by an error message I'm getting from my VS2005 compiler... "Cannot pass 'Item' as a ref or out argument because it is a 'foreach...
5
by: RacerX2000 | last post by:
I have an MS access Database (2000) and Have created a form that sets a variable to a value I would like (Based on other selections in the form) to pass to my query criteria and I get the following...
3
by: hon123456 | last post by:
Dear all, I have a session variable session("loginid) which can be passed from A.asp to B.asp. Then In B.asp I have a hyperlink to C.asp e.g. < A href..C.asp>. The session variable cannot be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.