hi guys, this is the scenario, i have two textboxes, then a submit button.
now, when the user clicks the submit button, it will verify if both textboxes have values,
otherwise, if only one of them is empty, an alert box will be displayed prompting the user to complete the information. this scenario is perfect and is working out fine. now, what i want to happen is that, the alert box will be displayed then the value inputted by the user on the textboxes will not be erased. how is that so?

depending on the method you use the submit the (if it's post or get) you can do something like this

<form action='index.php' method = 'post'>
   <input type = 'text' name = 'text1' value = '<?php 
       echo ( isset( $_POST['text1'] ) ?
       $_POST['text1'] : '' ) ?>' />

      // isset( $_POST['text1'] ) ? $_POST['text1'] : '' ) is the same as 
      // if(isset($_POST['text1'])){ echo $_POST['text1'] }else {}
</form>

the code does not work sir..

Try this

<?php


if($_POST['submit']=="Submit")
{
	$text1 = $_POST['text1'];
	$text2 = $_POST['text2'];
		
	if($text1 =="" || $text2=="")
	{
	?>
	<head>
		<script type='text/javascript'>
			alert('Some text here.');
		</script>
	</head>
	<?php
	}
       else
       {
       //what do you want when the two textbox have value
       }

}

echo "<form action='index.php' method='post'>";
echo "<input type='text' name='text1' value='$text1'>";
echo "<input type='text' name='text2' value='$text2'>";
echo "<input type='submit' name='submit' value='Submit'>";

?>

you can try this

<?php
if(isset($_POST['submit'])) {
	if(!empty($_POST['text1']) and !empty($_POST['text2'])){
	 //do what you want
        }
       else
       {
       /do youre alert
       }
}
?>

hope this help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.