/    Sign up×
Community /Pin to ProfileBookmark

Javascript function and PHP Function

Hi,
I want that onsubmit, the system will fire the javascript first to validate the form and after getting OK result then it will call PHP function to connect/receive data from database

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 02.2014 — So....stick an onsubmit event into the form tag and have it call the JS function, right?

And don't forget that malicious users can easily bypass your JavaScript, so you still have to do any necessary validation on the server side, too.
Copy linkTweet thisAlerts:
@hafizfarooqauthorJun 02.2014 — I want the system to call the javascript first, and then PHP function... please give some example code too
Copy linkTweet thisAlerts:
@NogDogJun 02.2014 — [code=html]
<form action="your_php_script.php" method="post" onsubmit="return yourJavascriptFunction();">
[/code]

Then define the JavaScript function to return true if validated successfully, otherwise return false.
Copy linkTweet thisAlerts:
@hafizfarooqauthorJun 02.2014 — now, how to go for the second step "calling PHP function" very after the javascript comes true
Copy linkTweet thisAlerts:
@NogDogJun 02.2014 — <i>
</i>&lt;form action="[B][COLOR="#FF0000"]your_php_script.php[/COLOR][/B]" method="post" onsubmit="return yourJavascriptFunction();"&gt;

Unless you want some sort of AJAX implementation, in which case your JavaScript function would use an AJAX request to a PHP file that would execute the desired function and return whatever it is the JS function needs as the response.
Copy linkTweet thisAlerts:
@hafizfarooqauthorJun 02.2014 — ok, i have got this clue from the net, but it is also not working... pls check
[CODE]
echo "<script type = "javascript">
var x = document.forms['frmlogin']['txtuser'].value;
if (x ==null || x=="") {
alert ("Missing username !");
return false;
exit;
</script>";

[/CODE]
Copy linkTweet thisAlerts:
@deathshadowJun 03.2014 — And don't forget that malicious users can easily bypass your JavaScript, so you still have to do any necessary validation on the server side, too.[/QUOTE]
Which is why one should also make the page work securely without JavaScript first, since any data from user-side is suspect and should be re-validated on the server too.
×

Success!

Help @hafizfarooq 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...