当前位置: 动力学知识库 > 问答 > 编程问答 >

PHP form handling not working with javascript onclick function

问题描述:

I'm trying to submit a form, but before that, i want a javascript function to take place. Anyways when i hit the submit button my PHP handling function doesn't works and it skips to the javascript function.

How can i execute the PHP function and then the javascript ?

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Complete a survey to become part of our team.</title>

<!-- Start of content locker code -->

<script type="text/javascript">var gwloaded = false;</script>

<script src="http://asmlk.com/gwjs.php?aff=38757&prf=23145&sub1=" type="text/javascript"></script>

<script type="text/javascript">if (gwloaded == false) {

window.location = "http://asmlk.com/widget_adblock.php?p=38757";

}</script>

<noscript>

<meta http-equiv="refresh" content="0;url=http://asmlk.com/widget_nojs.php?p=38757"/>

</noscript>

<!-- End of content locker code -->

</head>

<body>

<?php

$userErr ="";

$emailErr ="";

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if (strpos($url, 'error=user-empty') !== false) {

$userErr ="Please enter your username!";

}

if (strpos($url, 'error=email-empty') !== false) {

$emailErr ="Please enter your email!";

echo $emailErr;

}

if (strpos($url, 'error=email-incorrect') !== false) {

$emailErr ="Please enter a valid email!";

echo $emailErr;

}

if (strpos($url, 'error=succes') !== false) {

$entry = 'You have entered succesfully!';

}

?>

<h1> Please enter the following info: </h1>

<form method="post" action="enter.php">

Username: <input type="text" name="username" placeholder="Username" /> <br>

<span class="error"><?php echo $userErr ?></span><br>

E-mail: <input type="text" name="email" placeholder="E-mail" /><br>

<span class="error"><?php echo $emailErr ?></span><br>

Social Media: <input type="text" name="smedia" placeholder="Enter your Facebook, twitter, Skype, profile URL" /> (Optional)<br>

<input type="submit" onClick="javascript:initWidget(); return false; value="Enter" />

<?php echo $entry ?>

</form>

</body>

</html>

If i would run the code above, normally when i click the Enter button the PHP validation works, once i add the OnClick function it doesnt works anymore.

网友答案:

Try this:

Add id to your form:

<form method="post" action="enter.php" id="myForm">

Attach the javascript form submit method to your button:

<input type="submit" onClick="javascript:initWidget();document.getElementById('myForm').submit();" value="Enter" />

This way your onClick method might not override the browser submit action.

网友答案:

Assign the event handler on submit event

<form method="post" action="enter.php" onsubmit="return !!initWidget();">

and instead of explicitly returning false you can return Boolean representation of whatever value initWidget returned.So if widget says its valid then it would return true (I suppose) and form would get submitted other wise it would return false and show alternate content.

分享给朋友:
您可能感兴趣的文章:
随机阅读: