Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with PHP messages

Featured Replies

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. Thanks in advance. 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>

 

Very Simple Solution :-

 

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

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

 

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

 

By Xee Studios

check here

 

and here

 

u can do that by

making a new template for that and load new page on submit using header location ?

As ever you have come up with 2 more quality links that seem to be bang on subject, obviously you have spent a lot of time researching such points of reference.

 

Do you have a blog on your research methods? and if not WHY?

  • Author

Thanks for your reply. Adding those lines doesn't work because when I load the page the boxes dissappear so I can't type anything in. Any suggestions?

 

By the way. I don't want the messages to display on a different page I would like to keep everything on the one, same page.

Edited by gsingh85

As ever you have come up with 2 more quality links that seem to be bang on subject, obviously you have spent a lot of time researching such points of reference.

 

Do you have a blog on your research methods? and if not WHY?

i used to write blog... long back but did stop due to lack of time

i used to write blog... long back but did stop due to lack of time

Have you seen this recent thread on blogs?

 

http://www.webdesignerforum.co.uk/topic/72269-a-community-blog/?p=457962

 

Maybe a combined blog that experienced forum members contributed too when they have the time would be a good idea?

Please tell me this is a joke - the videos he posted are not quality at all. They are absolutely horrible examples of all the terrible things you should NOT be doing as a web developer.

 

Absolutely ignore those links if you want to get good at php. Instead, try some actually good resources like this: http://phptherightway.com

 

I'd recommend learning some frameworks like symfony or zend or laravel instead of wasting your time on crappy videos like this.

and u r recommending syphony n zend to a newbie php developer?

 

and the things discussed here http://www.developphp.com/ are all crap ?

 

 

can u point out wht exactly is crap there?

Edited by sash_oo7

Once again I have to agree with Sash; Although all developers need to understand and manipulate a vast array of frameworks, and if they can't get to grips with oop they may as well stay in bed, the only place to start is with the basics of php.

 

Once you have those in place, and understand what php can do without the power of a framework, including object orientated applications, then you can start on some of the simpler frameworks such as the original 'mvc' methods before expanding into more complicated libraries and frameworks. My next step was to Codeignighter, which was fun to experiment with even if it is outdated and unsupported by it's original developers.

  • Author

Right, but the videos he linked to were terrible examples of php regardless. Even if you don't want to use a framework and don't even want to use OOP, these are still really bad examples that will teach you literally nothing good and a load of bad practices that you will need to completely unlearn in order to progress with your own development.

 

I'd say take a stab at learning about objects, and why not just have a go at something like codeignitor earlier rather than later? You may surprise yourself.

 

 

I have to say I'm really disappointed to hear you say this.

I took a lot of time to learn this code from a developer who I thought was an

expert in php. As someone new to php I can’t really tell the difference between

good php code and bad php code. My general rule of thumb when trying to learn

php code is, is it flexible? Is it lightweight? Does it work? If the answer is

yes to all 3 then I will learn it and keep it for reference and re-use it. To

hear you say the code is badly written and outdated is quite disheartening.

 

 

 

This is code for creating a newsletter and I have tested it and the answer is a “yes” to the above 3 questions. If the code is so bad would you be able to post an equivalent newsletter code that is written with OOP in mind and I will study that and compare it to the “badly” written code. I can then use your code as a reference to good practise. Thanks in advance.

 

 

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.