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
https://forums.phpfreaks.com/topic/248097-using-javascript-in-an-if-statement/
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.

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>

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.

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'] ) ;

}

}

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.