Jump to content

Alert Box, Need Help!


yakoup46

Recommended Posts

So I am trying to use an alert box. I am using...

echo "<script>alert('Sorry Wrong!')</script>";

 

However, it brings up the alert and then sits on a blank page, which is obviously my PHP file. I want the alert box to open in the same page and not go the actual PHP file.

Link to comment
Share on other sites

Why are you using PHP to display Javascript. PHP is server side so it will process the code prior to it getting to the user. Javascript should be client side so use an event handler i.e

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script language="javascript">
function doSomething() {
alert('did you see what I did');
}
</script>
</head>
<body>
<a href="#" onClick="doSomething()">Click Me</a>
</body>
</html>

 

If you are using a form set the action to run your javascript validation function.

Link to comment
Share on other sites

Yeah I know how to do that but I want the display box on the same page.

So say I am at something.com, I hit sumbit, then what I want to happen is for a alert box to come up on the same page and stay on that page after I close it. I am using PHP to validate the forms so I am kinda forced to use Javascript in my PHP.

Link to comment
Share on other sites

What pages man?

 

You want help, but we dont know why you want it because you already have a php page as you say with the form on there why not just add the alert on it?

 

At least myself - am very confused with your question.

 

Example.php

<?php
// some code
?>
<form action="test.php" method="post">
<input type="submit" value="HIT ME!" onClick="Javascript:Alert('OMG I GOT HIT');">
</form>

 

Thats all i can think your asking for right now?

 

-cb-

Link to comment
Share on other sites

If you are submitting the form to the same page then it will remain on that page unless you are submitting to a different file or using a header to redirect.

You should not use javascript to display errors when you are using a server side script to validate, you will just see a blank screen with an alert box, the user will click ok and the page will be outputted. A simple bit of text near the input box where the user made the error is much better.

If you use Javascript to display errors then you should use Javascript to validate the form! Although you should still have server side validation as users can simply switch javascript off.

Link to comment
Share on other sites

Save the following as test.php and run on your server:

<?php
// process form
    $errors = array();
if(isset($_POST['submit']) && $_POST['submit'] == 'go') {
        if(!strlen(trim($_POST['name']))) {
        	$errors['name'] = true;
        }
        if(!count($errors)) {
        	// no errors
        }
   	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form methoid="post" action="test.php">
<?php if($errors['name']) print "<p>Please enter your name</p>"; ?>
Name: <input type="text" name="name" size="50" maxlength="50" /> <input type="submit" name="submit" value="go" />
</form>
</body>
</html>

Link to comment
Share on other sites

On the php form. Put if conditions where you want the notes to be displayed, then something like:

 

Form.php

<?php

 

// Check if form has been submitted:

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

  .. Validate etc

  if(isset($_GET['name'] && strlen($_GET['name']) > 3)){

    .. All ok..

  }else{

    // Something in form is wrong with name..

    $wrong_name = "Your Name is too short";

  }

}

 

// Your Form

?>

<input type="text" name="name" value="<?php echo($_GET['name']); ?>" />

<?php

// Echo what was wrong (above, it will keep the same value the user entered on the form sop they dont have to enter it again.

if(isset($wrong_name)){

  echo($wrong_name);

}

?>

 

More advanced santization systems can be made but this is a simple method that works per element.

 

-cb-

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.