Tiamo
Tiamo

Reputation:

Help With JavaScript Alert

function msg ($msg) {

 echo "<script type='text/javascript'>alert('$msg');</script>";


}

if (count($_POST)) {
   msg('TEST!!!!!');
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website</title>
<link rel="stylesheet" type="text/css" href="/assets/css/main.css" />   
</head>
<body>

<div id="content">
<form action="/user/register" method="post">

<table cellspacing="2" cellpadding="0">
<tr>

 <td>Username:</td>
 <td><input name="username" maxlength="25" size="25" type="text" />
 </td>
</tr>

<tr>
 <td>E-mail:</td>
 <td><input name="email" maxlength="255" size="25" type="text" /></td>
</tr>

<tr>
 <td>Password:</td>
 <td><input name="password" maxlength="255" size="25" type="password" /></td>
</tr>

<tr>
 <td>Confirm password:</td>
 <td><input name="password2" maxlength="255" size="25" type="password" /></td>
</tr>

<tr>

 <td>&nbsp;</td>
 <td><input type="submit" value="Register" /></td>
</tr>

</table></div>

</body>
</html>

When I post it doews show the alert but on a blank page. The form vanish when i press submit. How can I do so it still shows the the html?

The message pops up but on a blank page. I want it to still show the page your on

Upvotes: 0

Views: 169

Answers (2)

andres descalzo
andres descalzo

Reputation: 14967

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website</title>
<link rel="stylesheet" type="text/css" href="/assets/css/main.css" /> 
<script type='text/javascript'>
(function(){
   var countPost = <?php echo ((count($_POST)>0)?count($_POST):0); ?>;
   if (countPost>0)
      alert('msg test');
})()
</script>
</head>

Upvotes: 0

Tyler Carter
Tyler Carter

Reputation: 61567

First, You need to put the script in the tags, instead of above the DOCTYPE. Try this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My website</title>
<link rel="stylesheet" type="text/css" href="/assets/css/main.css" />   
<?php
if (count($_POST)) {
   msg('TEST!!!!!');
}
?>
</head>

After that, there might be more problems.

Upvotes: 2

Related Questions