Jump to content

using javascript in an IF statement


ukscotth

Recommended Posts

Hi,

 

I'm trying to work out how I can trigger a javascript popup type box in an if statement, to trigger it normally from a button it would be :

 

<input type="button" value="Display" onclick="javascript: formFunction();" class="submit" />

 

But I want to use it when people submit a form without filling in some of the fields so something like this :

 

if  (($_POST['first_name'] == '')) { run javascript popup }

 

Any ideas ?

 

Many thanks in advance.

 

Scott

Link to comment
Share on other sites

It aint pretty but it does what you want.

if  (($_POST['first_name'] == '')) { echo '<script type="text/javascript"> alert(\'this is an alert box\');</script>'; }

 

Wouldnt it be better to verify the data using javascript before it gets posted to the server and then floods the user with incomplete field popups? Just a thought.

Link to comment
Share on other sites

You can replace the alert box that I wrote with any other javascript function.

Facebook style popups will require HTML etc inside them so it should be left till the end of script execution..

AKA.. store all the errors (if any) when the page is loaded make your pretty little popup..

 

From a user stand point, its less intrusive.

 

Example:

if (isset($_POST['submitted'])) {
$errors = array();
// do all your error checking
}

 

Bottom of the page:

<?php if (isset($errors) && !empty($errors))  { ?>
<script type="text/javscript">
    javascriptErrorHandler('<?php echo join(',',$errors); ?>');
</script>
<?php } ?>
</body>
</html>

Link to comment
Share on other sites

ok hes just told me that a normal alert box is fine but when i do this it messes up the styling on the page

 


if(isset($_POST['submit'])){


if (($_POST['from'] == "") || ($_POST['to'] == "")){



}else{

header( 'Location:quote_part1.php?from='.$_POST['from'].'&to='.$_POST['to'] ) ;

}

}

any ideas why ?

thanks.

Link to comment
Share on other sites

sorry i meant to put

 

if(isset($_POST['submit'])){


if (($_POST['from'] == "") || ($_POST['to'] == "")){
?>
 <script type="text/javascript">
window.alert("You message goes here!")
</script>
<?php


}else{

header( 'Location:quote_part1.php?from='.$_POST['from'].'&to='.$_POST['to'] ) ;

}

}

Link to comment
Share on other sites

Problem solved.

 

I used this code to check the fields before they were submitted

 

<script language="javascript" type="text/javascript">
function checker()
{
var myForm = this.document.myForm;
	if(myForm.from.value == '')	
	    {
		     alert("Please Select the pick up area");
			 myForm.from.focus();
			 return false;
	    }


	if(myForm.to.value == '')
		{
			alert("Please Select the droping area");
			myForm.to.focus();
			return false;
		}

	if(myForm.from.value == myForm.to.value)
		{
			alert("Pick up area and droping area should not be same");
			myForm.to.focus();
			return false;
		}


}
</script>

 

Thanks alot for your help, its most appreciated :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.