php-display a form on if statement

Discussion in 'Web Design and Development' started by jordanste, Mar 3, 2008.

  1. jordanste macrumors member

    Joined:
    Feb 25, 2006
    #1
    hey folks,

    im trying to write a mailing list singup form with two required fields "email" and "zipcode". however, to make sure that they are not entered in as blank i figured i would write an if statement to determine whether or not there is a value for each field, this is what ive got:

    PHP:
    //...

        
    if ($register=='yes')
            {

            if (!isset(
    $_POST['email']) || empty($_POST['email']))
                {
                echo 
    "<table id="mailing_list_form" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>"//line 16
                
    echo "<form action='http://www.#.net/mailinglist/verify.php?register=yes' method=POST>";
                echo 
    "<table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="$email"></label><span class=error>**email is a required field</span></td></tr>";
                echo 
    "<tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="$zipcode"></label></td></tr>";
                echo 
    "<tr><td><input type="submit" value="register"></td></tr></table>";
                echo 
    "</form></fieldset></td></tr></table>";
                }

            if (!isset(
    $_POST['zipcode']) || empty($_POST['zipcode']))
                {
                echo 
    "<table id="mailing_list_form" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>";
                echo 
    "<form action='http://www.#.net/mailinglist/verify.php?register=yes' method=POST>";
                echo 
    "<table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="$email"></label></td></tr>";
                echo 
    "<tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="$zipcode"></label><span class=error>**zipcode is a required field</span></td></tr>";
                echo 
    "<tr><td><input type="submit" value="register"></td></tr></table>";
                echo 
    "</form></fieldset></td></tr></table>";
                }

            
    $query="INSERT INTO mailing_list (id,email,zipcode,regdate) VALUES (NULL , '$email', '$zipcode', '$date')";
            
    mysql_query($query) or die('<p>Error, insert query failed</p>');
        
            
    mysql_close($con);

            echo 
    "<p>Thank you for registering!</p>";

    the problem is that im getting a parse error "syntax error, unexpected T_STRING, expecting ',' or ';' ... on line 16." im not sure where the problem is, but i imagine it has to do with all of those echos
    is there a better way to redisplay the form if a field is empty? i.e. instead of using all of those echo echo echo echo...?

    thanks.
     
  2. web_god61 macrumors regular

    web_god61

    Joined:
    May 14, 2004
    #2
    i believe the problem lies here:

    echo "<table id="mailing_list_form"

    you need to use single quotes, when in double quotes, here and countinued in your echos the double quotes break the string, causing a parse error. I dont know if im explaining it correctly but replace the double quotes inside the echo string with single quotes should fix your problem.
     
  3. exabytes18 macrumors 6502

    Joined:
    Jun 14, 2006
    Location:
    Suburb of Chicago
    #3
    Replacing the double quotes with single quotes is one solution like web_god61 said.

    The double quotes interrupt the literal string you are trying to print out. To actually print out double quotes, you must add slashes to the quotes. They're called escape characters if you want to google more about them.

    You'll have to add slashes to the rest of your script, but it's done as follows:

    PHP:
    echo "<table id=\"mailing_list_form\" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>";
     
  4. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #4
    dont forget if its got a $something to use it like this

    example
    PHP:
    echo "<td>".$error."</td>";
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Actually, in PHP, you can use the variables inside double quotes, though I'm never completely comfortable doing it so generally do it your suggested way.
     
  6. alfaphlex macrumors newbie

    Joined:
    Feb 18, 2008
    #6
    Here's an alternative, faster and easier method to output all that html:

    PHP:
    <?php

            
    if (!isset($_POST['email']) || empty($_POST['email']))
                {
    ?>
    <table id="mailing_list_form width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend> 
    <form action='http://www.jordanstephens.net/mailinglist/verify.php?register=yes' method=POST>
    <table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="<?=$email ?>"></label><span class=error>**email is a required field</span></td></tr>
    <tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="<?=$zipcode ?>"></label></td></tr>
    <tr><td><input type="submit" value="register"></td></tr></table>
    </form></fieldset></td></tr></table>

    <?php
                
    }

            if (!isset(
    $_POST['zipcode']) || empty($_POST['zipcode']))
                {
    ?>
    <table id="mailing_list_form" width=250><tr><td><fieldset><legend><b>Mailing List!</b></legend>
    <form action='http://www.jordanstephens.net/mailinglist/verify.php?register=yes' method=POST>
    <table><tr><td><label>e-mail:</td><td><input type=text name='email' size=20 maxlength=40 value="<?=$email ?>"></label></td></tr>
    <tr><td><label>zipcode:</td><td><input type=text name='zipcode' size=5 maxlength=5 value="<?=$zipcode ?>"></label><span class=error>**zipcode is a required field</span></td></tr>
    <tr><td><input type="submit" value="register"></td></tr></table>
    </form></fieldset></td></tr></table>

    <?php
                

    When outputting big chunks of html, it's easier+faster to just write it as html and not have php echo out so much of it. It also helps you because you don't have to deal with escaping single/double quotes. Any variable you wanna insert into the html chunks, you can use the quickhand echo method which is basically
    PHP:
    <?=$variable ?>
    No need to use a semi-colon. Look in the value="" section of the html above to see how to apply the quickhand method. Hope this tip helps in any way.
     
  7. jordanste thread starter macrumors member

    Joined:
    Feb 25, 2006
    #7
    awesome, hey thanks everybody for the help. i got it working the way i want it to and i learned alot about php in the process. im still really new to this so thanks again.
     

Share This Page