/    Sign up×
Community /Pin to ProfileBookmark

JavaScipt onSubmt will not work with PHP?

Hello everyone:

I used a javascript file to validate user input before form can be submitted.

I used extract function to extract value being submitted to php file. My problem is that after user click the submit button, php script won’t display information being submitted.

I commented out link to javascript link, Submitted information displayed. This make me think can Javascript and PHP co-exist together? I want to use JavaScript to do client-side validation first, then using PHP file to display submitted data.

my project consists of three file a html file, a javascript file and one php file.
if user input an invalid entry on the form, javascript should prompt alert message. If form entry is valid php script should display data submitted.

Please look at my code and give me some suggestion how to make it work. Thanks!

html file code:

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3. org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>
<title>Computer Center Account Application Form</title>
<script src=”form_validation.js” type=”text/javascript”></script>

</head>

<body onLoad=”document.account_form.fname.focus()”>
<form name=”account_form” id=”account_form” action=”form_info.php” method=”post” onSubmit=”return form_validation()”>

First Name:
<input type=”text” name=”fname” id=”fname” size=”15″ maxlength=”15″ /><br /><br />

Last Name
<input type=”text” name=”lname” id=”lname” size=”15″ maxlength=”15″ /><br /><br />

<p>
<input type=”submit” value=”submit” />
<input type=”reset” value=”reset”/>
</p>

</form>
</body>

</html>
[/CODE]

Javascript Code

[CODE]function form_validation()
{

var fnameOK = false;
lnameOk = false;

var first_name_field = document.getElementById(“fname”);

if(fnameOK)
return true;
else
return false;

}//end form_validation fn

function checkFirstName(elementReference)
{
if(elementReference.value == “”)
{
alert(“First Name field is blank”);
return false;
}
else if(elementReference.value.length < 2 || elementReference.value.length > 15)
{
alert(“First Name field should between 2 to 15 characters”);
return false;
}
else
return true;

}
[/CODE]

PHP code

[code=php]
<?php

extract($_POST); //extract data from $_POST array

$fname = stripslashes($fname);
$lname = stripslashes($lname);

//mail the form information

print ($fname.”<br />”);
print ($lname.”<br />”);

?>
[/code]

webdev077
6-12-07

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarJun 13.2007 — that exact script wont do anything lol.

your name check at the moment always evaluates false meaning that the form never gets submited to PHP, try this small rework:

(in PHP tags only forthe highlighting, this is javascript!!)[code=php]
function form_validation(){
var valid = true;
if(document.getElementById("fname").value.length < 2
|| document.getElementById("fname").value.length > 15)
valid = false;
if(document.getElementById("lname").value.length < 2
|| document.getElementById("lname").value.length > 15)
valid = false;
return valid;
};[/code]
Copy linkTweet thisAlerts:
@webdev077authorJun 13.2007 — Hello scragar:

Thanks for pointing out my problem. I rechecked my code, I forget to call the validation function inside form_validation fuction in javascript file. That caued the error!:mad:

I added in function call as follows:

[CODE]
var fnameOK = false;
var first_name_field = document.getElementById("fname");

fnameOK = checkFirstName(first_name_field);[/CODE]




your name check at the moment always evaluates false meaning that the form never gets submited to PHP, try this small rework:
[/QUOTE]
×

Success!

Help @webdev077 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...