Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Mailing List Creation

    Hi Guys,

    Whilst our PHP developer is on his honeymoon I have been given a project to finish.

    My PHP knowledge is basic to say the least so I thought I would seek some expert help.

    I have created a database called "Mailing List" inside this database is a table called "Information" the fields inside this are called "id" and "email".

    The form for this database has an "Enter Your Email Address" once submitted it stores the email in the database, so that the company using the database can build up a database of client emails. Simple I hear you say.

    As well as this they need a check box which says "Do you agree for us to store your email address blah blah."

    If they tick this box and hit submit, I need it to store the email address and then forward them onto another page. But also, if they choose not to agree then it doesn't store the email but still forwards them to the next page.

    So there is the brief.

    As my knowledge of PHP is so basic I was wondering if somebody could help me create this. Will I need to use just javascript for the checkboxes or will I need another field in the database???


    If somebody could help that would be great, and you would be a saviour, a PHP Jesus Christ.


    Cheers

    Danny

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    So you have the part where they store it ... that's already done?
    Now you just need to add the checkbox part?

    You will have the checkbox on your form.

    <input type="checkbox" name="ch1"> Do you agree for us to store your email address blah blah.<br />

    In the script where you process the form and save the data,
    you would test for that checkbox. You're merely checking to
    see if the variable is set or not. An unchecked checkbox is null,
    meaning within the PHP script, it has no value and is not set.

    if(isset($_POST['ch1'])){
    // save the data
    }

  • #3
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi

    I haven't done the part where it stores the email. But I think I will be able to do it.

    So does the checkbox field need to be added into the database?

    If not then I will use your bit of code, but then once one is selected or not selected I want to forward them on to the next page. How do I do that?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    If that decision (checkbox) only needs to be made one time,
    you don't need it the database. If you need to remember that
    they checked it, for other reason, then you would need to
    add it. If you do add it to the database, you'll have to determine
    if you want to make that field a "0" or a "1", or ... "Y" or "N" ... whatever.


    // if the checkbox is NOT set, redirect to the other page.
    if(!isset($_POST['ch1'])){
    header ("location: nextpage.php");
    exit;
    }
    // otherwise, save the info in the database here
    //
    //
    // then go to the next page ...
    header ("location: nextpage.php");

  • #5
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi

    Thanks for the tips on the checkbox. The checkbox will only need to be checked when they come to the page. It is just so that our client covers there bases, and gives the cutomer a chance to opt out of saving the email address.

    I had a bit of code that I was going to use for the adding of the email to the database, But I cannot get that to work. As I said before I am know programmer.

    Roughly the script was like the following

    PHP Code:
    <?PHP
    // Check if the form has been submitted.
    if (isset($_POST['submitted'])) {

            require_once(
    '../../xxxxxxxxxx.php'); // Connect to the database.
        
    $errors = array(); // Initialise error array.
        
        // Check for anEmail.
        
    if(empty($_POST['email'])) {
            
    $errors[] = 'You forgot to enter an Email Address.';
        } else {
            
    $e = ($_POST['email']);
        }
    if(empty(
    $errors)) { // If everything's OK.
        
            // Enter the details into the database.
            
            // Make the query.
            
    $query "INSERT INTO Information (email) VALUES ('$e')";
            
    $result = @mysql_query ($query); // Run the query.
            
    if ($result) { // If it ran OK.
                    // Print a message.
                    
    echo '<h1>Thank You!</h1>
                    <p>You have added your email.</p><p><br /></p>'
    ;
            } else {
                echo 
    '<h1>System Error</h1>
                <p>You could not enter an Email due to a system error. Sorry for the inconvenience.</p>'
    // Public Message.
                
    echo '<p>' mysql_error() . '</br /><br />Query: '$query '</p>'// Debugging Message.
            
    exit();
        }    
            
    mysql_close(); // Close the database conneciton.
        
    } else { // Report the errors.
            
    echo '<h1>Error!</h1>
            <p>The following Error(s) occurred:<br />'
    ;
            foreach (
    $errors as $msg) { // Print each error.
                
    echo " - $msg<br />\n";
            }
            echo 
    '<p></p>Please Try Again.</p><p><br /></p>';
        } 
    // End of if(empty($errors) IF.    
    // End of the main Submit Conditional.
    ?>

    Then inside the form I was going to put the following

    In the email field value area
    PHP Code:
    <?PHP if(isset($_POST['Email'])) echo $_POST['Email']; ?>
    I am not sure where the checkbox bit would go but I am assuming just after checking the email.


    Any help or re-write would be great.

    Cheers Danny

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    So there is a form somewhere that they enter in their email address?
    It's on that form where you put the checkbox.

    <input type="checkbox" name="ch1"> Do you agree for us to store your email address blah blah.<br />

    This is the altered script ...

    PHP Code:
    <?PHP 
    // Check if the form has been submitted. 
    if (isset($_POST['submitted'])) { 

            require_once(
    '../../xxxxxxxxxx.php'); // Connect to the database. 
        
    $errors = array(); // Initialise error array. 
         
        // Check for anEmail. 
        
    if(empty($_POST['email'])) { 
            
    $errors[] = 'You forgot to enter an Email Address.'
        } else { 
            
    $e = ($_POST['email']); 
        } 
    if(empty(
    $errors)) { // If everything's OK. 
         
    //only enter details if the checkbox is NOT checked.
    if(!isset($_POST['ch1'])){

            
    // Enter the details into the database. 
             
            // Make the query. 
            
    $query "INSERT INTO Information (email) VALUES ('$e')"
            
    $result = @mysql_query ($query); // Run the query. 
            
    if ($result) { // If it ran OK. 
                    // Print a message. 
                    
    echo '<h1>Thank You!</h1> 
                    <p>You have added your email.</p><p><br /></p>'

            } else { 
                echo 
    '<h1>System Error</h1> 
                <p>You could not enter an Email due to a system error. Sorry for the inconvenience.</p>'
    // Public Message. 
                
    echo '<p>' mysql_error() . '</br /><br />Query: '$query '</p>'// Debugging Message. 
            
    exit(); 
        }     
            
    mysql_close(); // Close the database conneciton. 
        
    } else { // Report the errors. 
            
    echo '<h1>Error!</h1> 
            <p>The following Error(s) occurred:<br />'

            foreach (
    $errors as $msg) { // Print each error. 
                
    echo " - $msg<br />\n"
            } 
            echo 
    '<p></p>Please Try Again.</p><p><br /></p>'
        } 
    // End of if(empty($errors) IF.     
    // End of the main Submit Conditional. 
    }// End of checking for checkbox
    ?>

  • #7
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi

    I have dropped in the code which you suggested, but I am assuming I have a syntax error somewhere as when I hit the submit button the page just goes to a white screen.

    Here is the code

    Code:
    <?PHP  
    // Check if the form has been submitted.  
    if (isset($_POST['submitted'])) {  
    
            require_once('xxxxxxxxxxxxx.php'); // Connect to the database.  
        $errors = array(); // Initialise error array.  
          
        // Check for anEmail.  
        if(empty($_POST['email'])) {  
            $errors[] = 'You forgot to enter an Email Address.';  
        } else {  
            $e = ($_POST['email']);  
        }  
    if(empty($errors)) { // If everything's OK.  
          
    //only enter details if the checkbox is NOT checked. 
    if(!isset($_POST['ch1'])){ 
    
            // Enter the details into the database.  
              
            // Make the query.  
            $query = "INSERT INTO Information (email) VALUES ('$e')";  
            $result = @mysql_query ($query); // Run the query.  
            if ($result) { // If it ran OK.  
                    // Print a message.  
                    echo '<h1>Thank You!</h1>  
                    <p>You have added your email.</p><p><br /></p>';  
            } else {  
                echo '<h1>System Error</h1>  
                <p>You could not enter an Email due to a system error. Sorry for the inconvenience.</p>'; // Public Message.  
                echo '<p>' . mysql_error() . '</br /><br />Query: '. $query . '</p>'; // Debugging Message.  
            exit();  
        }      
            mysql_close(); // Close the database conneciton.  
        } else { // Report the errors.  
            echo '<h1>Error!</h1>  
            <p>The following Error(s) occurred:<br />';  
            foreach ($errors as $msg) { // Print each error.  
                echo " - $msg<br />\n";  
            }  
            echo '<p></p>Please Try Again.</p><p><br /></p>';  
        } // End of if(empty($errors) IF.      
    } // End of the main Submit Conditional.  
    }// End of checking for checkbox 
    ?> 
    <h1>Motorbike Theory Test - For FREE!</h1>
    <h3>Simply enter your email address and continue to the FREE Motorcycle Theory Test.</h3>
    <form action="email.php" method="post">
    <fieldset><legend>Enter your email address to practice the all4bikers.com free motorbike theory test:<br /><br /></legend>
    <input type="text" name="email" size="40" maxlength="60" value="<?PHP if(isset($_POST['email'])) echo $_POST['email']; ?>" /><br />
    
    <p>Agree:&nbsp; Yes:<input type="checkbox" name="ch2" value="1" />&nbsp; No:<input name="ch1" type="checkbox" value="0"/></p>
    </fieldset><br /><br />
    <input type="submit" value="Continue" name="submitted" />
    <input type="hidden" name="submitted" value="TRUE" />
    </form>
    If somebody knows why this is not working then please let me know.

    Thanks Danny

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    In any case, it should at least display the form again.

    This line needs to be changed from this:
    <p>Agree:&nbsp; Yes:<input type="checkbox" name="ch2" value="1" />&nbsp; No:<input name="ch1" type="checkbox" value="0"/></p>

    To this:
    <p>Agree:&nbsp; Yes:<input type="checkbox" name="ch1" checked />&nbsp; No:<input name="ch2" type="checkbox" /></p>

    As far as the blank page, there must be an error that is not being displayed
    because your PHP configuration is not set up to display errors.

    I'm searching for a possible error, but can't see it.

    These are the only three lines I added to your script ...
    //only enter details if the checkbox is NOT checked.
    if(!isset($_POST['ch1'])){
    }// End of checking for checkbox


    Try removing them and see if the error goes away.

  • #9
    Regular Coder
    Join Date
    Jul 2008
    Location
    Blackpool, UK
    Posts
    176
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi

    Thanks for all your help, I managed to sort out the problem, it was to do with the connection. It all works great.


    Cheers for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •