/    Sign up×
Community /Pin to ProfileBookmark

Javascript Validation in PHP

Hi
I have a form that with the the following code:

[code=html]<form method=”post” action=”http://mydomain.org/xxx.php”>[/code]

I need to validate two fields within the form and I am using javascripts alert method to do so. However, when I click my Submit button, the form validation is executing and I get an alert on the php page referenced above. I want my alert box to come up on the current window location.

How should I write my javascript validation code? I currently have the following code:

[QUOTE]

<?php if($name == ”)
{echo “<script language=’javascript’>alert(‘You have not entered an name, please try again’)</script>”;}?>

[/QUOTE]

Thanks!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 01.2009 — You use javascript for client side validation, not server side.
&lt;form method='post' [b]onsubmit='Validate(this)'[/b]...
function Validate(frm){
var errors = array();
if(frm.getElementById('firstName) == '')
errors.push('Please enter your first name');
if(frm.getElementById('lastName) == '')
errors.push('Please enter your last name');

if(errors.length){
alert("Please fix the following errors:nn"+errors.join("n"));
return false;
}else
return true;
}

You will still need some server side validation though.
Copy linkTweet thisAlerts:
@StevishApr 01.2009 — If you wan javascript to validate, then it has to be written in javascript, not php. Basically you need to make a js function and have the submit button run that function (instead of submitting the form). Then if it passes the js validation, js will submit the form:

[code=html]<form method="post" action="http://mydomain.org/xxx.php" id="myform">
Name: <input type="text" inputName="name" /><br/>
<input type="button" value="Submit" onClick="checkForm()" />
<script type="text/javascript">
function checkForm() {
theForm = getElementById("myform");
if(theForm.inputName.value == '') {
alert('You have not entered an name, please try again');
var error = true;
}
if(error != true) {
theForm.submit();
}
}
</script>[/code]
Copy linkTweet thisAlerts:
@StevishApr 01.2009 — Scragars method looks better than mine...
Copy linkTweet thisAlerts:
@scragarApr 01.2009 — Stevish, your code is so very flawed. Why run validation onclick when you can attach it to the form and have it run regardless of the submit method.

And that many alert boxes would have me never visiting your site again, if you need more than 1 alert box to say one thing then you have messed up.


EDIT: This post makes me sound like a jerk, I never meant it that way, I was simply trying to point out the two serious flaws in the code Stevish posted that would have a serious negative impact on the scripts performance(and we are ignoring the "what happens if I disable javascript" problem here). If those things were to be fixed I can't imagine his code being considered worse than mine, in fact his code is a little more efficient in a few ways.
Copy linkTweet thisAlerts:
@StevishApr 01.2009 — Thanks for pointing that out. I'm pretty new to javascript, and try to avoid it when at all possible. I didn't know there was another method of validation, so my method was all I'd ever used. I guess I have some editing to do ?
Copy linkTweet thisAlerts:
@StaceyBauthorApr 01.2009 — Thanks guys for the response. I am getting confused on how to implement your suggestions. I using a cms and the html is separate from the php code. How can I do this<form method='post' onsubmit='Validate(this)'...[/QUOTE]

Can you provide an example?

(Hope I explained it correctly)
Copy linkTweet thisAlerts:
@StaceyBauthorApr 01.2009 — Disregard last post.. gotta spend more time on the basics.?

Thanks for the help.
×

Success!

Help @StaceyB 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.29,
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,
)...