Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-28-2010, 10:01 AM   PM User | #1
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Need help with mailing list script.

Hi Guys,

I am wanting to create the simplest script possible but I cannot do it. I also cannot find any tutorial online which matches what I need. So I thought you helpful souls here could give me a hand.


What I need

I have a form with TWO fields.

1. Name.
2. Email Address.

When the form submits it adds Name and Email address to a database called 'mailinglist'.

Directly above the form I have two divs one is a red box and one is a greenbox. Obviously if they enter something incorrectly the div which is red will appear, or if they enter everything correctly then when they click submit it adds them to db and shows them the green div saying you have been added.

Could anybody help me create this? I would really appreciate it. Whilst I understand the logic behind PHP and know what I need to do I cannot execute it.


I look forward to the help.

Cheers

Dan

Last edited by winnard2008; 04-29-2010 at 04:55 PM..
winnard2008 is offline   Reply With Quote
Old 04-28-2010, 10:33 AM   PM User | #2
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
http://www.codingforums.com/showthre...244#post949244
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Old 04-28-2010, 04:52 PM   PM User | #3
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Hi Phil,

Thanks again, will have a look into this tomorrow when I get chance.


Cheers
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 01:03 PM   PM User | #4
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Just to let you know.

I found your snippet for the email form without email. Added it and it worked, except for once you hit submit it strips my css away from the page so all you see is like plain text with no styling.

If you could assist with helping me make it so that it adds the success or alert part without losing the css that would great


Dan
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 01:40 PM   PM User | #5
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
I have narrowed the problem down to

define return address part and also the header(location) part of the script which is causing me the problem.

It is adding the msg to the url in the browser which has either a div class for success or an alert for errors added to the browser url rather than to the page itself.
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 02:05 PM   PM User | #6
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Any ideas anyone?
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 02:05 PM   PM User | #7
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
lets have a look at how you have used the code my friend and also the form used to send the data to.

Regards, Phil

p.s. if you are having problems with the script ( as i stated I didn't test that code fully sorry, will get around to it) comment out the header() and see what errors you are getting.
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Old 04-29-2010, 02:19 PM   PM User | #8
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Hi Phil,

The code works fine and submits the info entered into the form, it is just when you submit the form it strips away the css because it is showing a different address in the browser one which reads

Code:
http://localhost/rethink/index.php/?msg=%3Cdiv+class%3D%22alert%22%3E%3Cp%3EYour+Name+and+or+Email+Address+is+incorrect%2C+please+check+and+try+again.%3C%2Fp%3E%3C%2Fdiv%3E%3Cbr+%2F%3E%3Cbr+%2F%3E
I have a page called form.php which has the following part of your script in

PHP Code:
<?PHP 
define
"SUBMIT_BUTTON_NAME"'submitted' );
define"DB_SERVER"'localhost' );
define"DB_USERNAME"'root' );
define"DB_PASSWORD"'password' );
define"DB_NAME"'rethink' );
define"TABLE_NAME"'mailinglist' );
define"RETURN_ADDRESS"'index.php' );
define"MSG"'<div class="success"><p>Thanks! Your now in the loop.</p></div><br/><br />' );
?>
I then have the index.php page which has the following php

PHP Code:
<?php
include 'form.php';

$master = array( 
    
'input_name' => array( 
                          
'name'
                          
'email'
                    ), 
    
'regex' => array( 
                     
'',
                     
'^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})$'
                     
                ),
    
'error' => array( 
                
'<div class="alert"><p>Your Name and or Email Address is incorrect, please check and try again.</p></div><br /><br />' 
    
                
), 
    
'db_col' => array( 
                      
'NAME'
                      
'EMAIL' 
                      
                
)
);
if( isset( 
$_POST[SUBMIT_BUTTON_NAME] ) ) {
    
$CON mysql_connectDB_SERVERDB_USERNAMEDB_PASSWORD ) or die( "Error: on line " __LINE__ " located at " __FILE__ " " .  mysql_error() );
    
mysql_select_dbDB_NAME$CON );
    function 
filter$data ) {
        
$data trimhtmlentitiesmb_convert_encoding$data'HTML-ENTITIES'"UTF-8") ) ); 
        if ( 
get_magic_quotes_gpc() ) { 
            
$data stripslashes$data ); 
        } 
        
$data mysql_real_escape_string$data ); 
        return 
$data;
    }
    foreach( 
$_POST as $key => $val ) {
        
$post[$key] = $val;
        unset( 
$_POST[$key] );
    }
    foreach( 
$master['input_name'] as $key => $input_name ) {
        if( ! isset( 
$post[$input_name] ) || empty( $post[$input_name] ) ) {
            
$msg $master['error'][$key];
            break;
        }elseif( ! empty ( 
$master['regex'][$key] ) && ! preg_match"#" $master['regex'][$key] . "#is"$post[$input_name] ) ) {
            
$msg $master['error'][$key];
            break;    
        }
    }
    if( ! isset( 
$msg ) ) {
        
$col_str '';
        
$val_str '';
        foreach( 
$master['db_col'] as $key => $col_name ) {
            
$col_str .= "`" $col_name "`, ";
            
$val_str .= "'" $post[$master['input_name'][$key]] . "', ";
        }
        
$insert_query "INSERT INTO `" TABLE_NAME "` ( " substr$col_str0, -) . " ) VALUES ( " substr$val_str0, -) . " )";
        
mysql_query$insert_query$CON ) or die( "Error: on line " __LINE__ " located at " __FILE__ " " .  mysql_error() );
        
$msg MSG;
    }
    
mysql_close$CON );
    
header"location: " RETURN_ADDRESS "/?msg=" urlencode$msg ) );
    exit();
}
?>
I then have the following form layout.

Code:
<form class="newsletter-form" method="post" action="index.php">
		<?PHP 
		if( isset( $_GET['msg'] ) ) {
    			echo $_GET['msg'];
				}
		?>
		<p>Let us keep you in the know, with all our latest products and great monthly printing offers.</p><br />
		<label>Name:</label><span class="info" title="Enter Your Name: EG John Doe"><img src="images/info.png" alt="Enter Your Name" /></span>		        <br />
		<input class="text" type="text" name="name" size="30" maxlength="30" /><br /><br />
		<label>Email:</label><span class="info" title="Enter Your Email. EG. example@hotmail.co.uk" ><img src="images/info.png" alt="Enter Your Email" /></span><br />
		<input class="text" type="text" name="email" size="30" maxlength="100" /><br /><br />
		<input type="image" class="signup" src="images/button2.png" name="submitted" />
		<input type="hidden" name="submitted" value="TRUE" />
		</form>
In the form where it has the $_GET info is where I would like to display the Success MSG or the Array Errors in there respective DIVS.
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 02:19 PM   PM User | #9
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Oh and by the way, when I comment out the header part it simply shows a white blank screen.
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 02:21 PM   PM User | #10
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
thats good then! use

PHP Code:
<?PHP 
if( isset( $_GET['msg'] ) ) {
    echo 
urldecode($_GET['msg']);
}
?>
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Old 04-29-2010, 02:29 PM   PM User | #11
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
That is resulting in the same stripped down page and putting the info in the url and not the divs in the form area.

Here is a quick before and after submit images.

Before Submit



After Submit


Can you see what I mean.
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 02:33 PM   PM User | #12
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
give me a source code screen shot ( mainly the head section ) after you have posted. It seems as tho you are putting the result code in the wrong place... i think

p.s. love the background and logo ( had a look at your site, shame it's under construction )
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 

Last edited by Phil Jackson; 04-29-2010 at 02:36 PM..
Phil Jackson is offline   Reply With Quote
Old 04-29-2010, 02:46 PM   PM User | #13
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Hi Phil,

Here is the source of the form once submitted. As you can see it is adding the div but putting \ in between and after the class.



The head of the page after submit does not show anything.

The page is currently under construction because I am working on it, hence the form. Glad you like the logo etc.

Hopefully I will be nearly ready to get the site live once I have got your form working.

Cheers

Dan
winnard2008 is offline   Reply With Quote
Old 04-29-2010, 02:53 PM   PM User | #14
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
PHP Code:
<?PHP 
if( isset( $_GET['msg'] ) ) {
    echo 
stripslashes(urldecode($_GET['msg']));
}
?>
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Old 04-29-2010, 03:02 PM   PM User | #15
winnard2008
Regular Coder

 
Join Date: Jul 2008
Location: Blackpool, UK
Posts: 176
Thanks: 4
Thanked 0 Times in 0 Posts
winnard2008 is an unknown quantity at this point
Hi Phil,

It is still not working. I think it is because it is adding the message to the url in the browser rather than just posting to itself.

What do we need to change in order to get rid of the added info in the url input
winnard2008 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:28 AM.


Advertisement
Log in to turn off these ads.