Jump to content

Help with PHP messages


gsingh85

Recommended Posts

I have a simple newsletter sign up script that I'm using. I built it using a tutorial and because it's simple and basic I'm able to tweak it here and there as and when I need to.

 

There are some messages such as "?? is already on the system" and "please enter an email address". All these work fine however when I display certain messages such as "is already on the sytem" or "you have been added sucessfully" I want the text boxes to disappear. I've been looking online and I haven't found anything which is most likely because I don't know the correct terminology of what I'm looking to do. I only what the boxes to dissappear when certain messages are display.

 

Can someone please help me with this or direct me to a tutorial. Here is the script:

<?php

$name = "";
$email = "";
$msg_to_user = "";
if (isset($_POST['name']) && $_POST['name'] != "") {
   
    include_once "connect_to_mysql.php";
        
    // Be sure to filter this data to deter SQL injection, filter before querying database
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);
    
    if (!$email) {
        
        $msg_to_user = '<br /><br /><h4 class="header">Please type an email address ' . $name . '.</h4>';
        
        }
        
        else if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
        $msg_to_user = '<br /><br /><h4><font color="FF0000">invalid email address.</font></h4>';
    }

    else if ($numRows > 0) {
        
        $msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>';
    
        }
    else {
        
        $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime)
                                                    VALUES('$name','$email',now() )")  or die (mysql_error());
        
         $msg_to_user =  '<br /><br /><h4>Thanks ' . $name . ', you have been added successfully.</h4>';
        $name = "";
        $email = "";
    }
    
}

?>

</div>

<html>
<head>

<style>
.header {
color:#D2691E;
background-color: #000000;

}

</style>
<title>Subscribe to Our Newsletter</title>
</head>

<body>

<form style="width:440px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left; padding:24px;">
<legend>Subscribe to Our Newsletter  </legend>
<br />
Name:<br />
<input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" /><br />
Email:<br />
<input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" /><br /><br />
<input name="mySubmitBtn" type="submit" value="Submit">
<?php echo $msg_to_user; ?>
</fieldset>
</form>


</body>
</html>

By the way I tried:

 

******************************************************************************************************************************

<body>
<form style="width:440px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left; padding:24px;">
<legend>Subscribe to Our Newsletter  </legend>
<br />
<?php if($msg_to_user !=''){ ?>

 

Name:<br />
<input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" /><br />
Email:<br />
<input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" /><br /><br />
<input name="mySubmitBtn" type="submit" value="Submit">
<?php } ?>

<?php echo $msg_to_user; ?>
</fieldset>
</form>
</body>

 

******************************************************************************************************************************

 

but it doesn't work. All that happens is when the page loads up the sign up boxes are not there. Any help will be much appreciated as I've been trying to find a solution for this over the last 3 days. Thanks in advance.

Edited by Maq
Link to comment
Share on other sites

$msg_to_user = "";

When the page is first loaded, this variable remains empty.

On the second page load, this variable gets set - or should.

Thus we should be checking not for:

<?php if($msg_to_user !=''){ ?>

but for:

<?php if($msg_to_user == ''){ ?>

which would insert the name and address boxes into the form. 

 

You can also set flags (variables of either true of false) for each section of code and then check that value before displaying that particular html section.

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.