• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Very basic questions about PHP

Can you not have PHP on the same page as your HTML?

For example, lets say I have my index.php. And, lets say that I have all of the design of my page. Does the PHP code work like ColdFusion where it can come in and out as it pleases? Because, I have some PHP code that has HTML code inside of it, and it is showing up on the page, but I don't want it to be there...

I hope someone understands what I am saying, thanks! :whiste:
 
Last edited:
You can't have HTML in your PHP, but as easily as you can have PHP in your HTML. In a PHP page, PHP code goes inside <?php ... ?> tags. You can have many of these. Outside of them you can put HTML. You can also stick the value of a PHP expression into the HTML with something like <?=$variable?>.

I marked out the "but" because you can also echo HTML inside your PHP. But placing static HTML outside the <?php ... ?> tags is usually better.

Perhaps you have a "?>" inside your PHP that's making the parsing confused?
 
Sorry about the vague OP. Thanks for the response, Ken G6 (love your GM scripts, btw).

Anyways, here is some PHP/HTML I wrote. This is all inside of the <body></body> tags:

Code:
        <?php
            
        $con = mysql_connect('test.net','test','test');
        mysql_select_db('test', $con);
        
        /*
        '
        ' Use this for testing.
        '
        if (!$con)
        {
            die('Could not connect: ' . mysql_error() . '<br /'); 
        }
        else { echo 'Connection established!<br />'; }*/
        
        /*
        ** Check to see if the e-mail address already exists in the table.
        */
        $sql = "SELECT * FROM MailingList WHERE eMailAddress = '$_POST[email]'";
        $result = mysql_query($sql, $con);
        
        
        if (!mysql_result($result, 2) == $_POST[email])
        {
            $insertSQL = "INSERT INTO MailingList (FirstName, LastName, eMailAddress, ZipCode) VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[email]', $_POST[zipcode])";
            mysql_query($insertSQL, $con);
            echo "Thank you for subscribing to our mailing list, " . $_POST[fname] . "!";
        }
        else { echo $_POST[email] . " is already subscribed to our mailing list."; }
    ?>

    <strong>Please fill out the form below to subscribe to someone's electronic mailing list!</strong><br />
  
    <form action="index.php" method="post" name="mailForm">
        <table>
            <tr><td align="right">First Name:</td> <td><input type="text" name="fname" maxlength="10" /></td></tr>
            <tr><td align="right">Last Name:</td> <td><input type="text" name="lname" maxlength="10" /></td></tr>
            <tr><td align="right">E-mail Address:</td> <td><input type="text" name="email" maxlength="30" /></td></tr>
            <tr><td align="right">ZIP Code:</td> <td><input type="text" name="zipcode" maxlength="5" /></td></tr>
        </table>
        <input type="submit"/>
    </form>

The line that states:

Code:
        else { echo $_POST[email] . " is already subscribed to our mailing list."; }

is always outputted to the page -- even on initial load. It will just say "is already subscribed to our mailing list".

Hope that makes a little more sense...thank you again.
 
Then this:
PHP:
(!mysql_result($result, 2) == $_POST[email])
is always returning False. This might be why. 😉
 
It works like it should when you enter an e-mail. So, if it finds someone in the database already, it won't add it, but otherwise it will. Its just that initial load that is driving me mad!

I'll check out your link to hopefully find a solution -- thanks!
 
Surprising, but maybe that changed in a more recent version of PHP.

So what you're saying is that in one case it correctly goes to the "if" clause of the if-else, in another case it correctly goes to the "else" case, but you want it to do something else in a third case? :sneaky:
 
Yes, pretty much.

The first time that a person goes to the page, I don't want it to say "is already on our mailing list". I'm trying to use two IF statements instead of an IF/ELSE, but that's not working either... 🙁
 
Does this work?
Code:
    <?php
        if ($_POST[email] != NULL)
        {    
            $con = mysql_connect('test.net','test','test');
            mysql_select_db('test', $con);
        
            /*
            '
            ' Use this for testing.
            '
            if (!$con)
            {
                die('Could not connect: ' . mysql_error() . '<br /'); 
            }
            else { echo 'Connection established!<br />'; }*/
        
            /*
            ** Check to see if the e-mail address already exists in the table.
            */
            $sql = "SELECT * FROM MailingList WHERE eMailAddress = '$_POST[email]'";
            $result = mysql_query($sql, $con);
        
        
            if (!mysql_result($result, 2) == $_POST[email])
            {
                $insertSQL = "INSERT INTO MailingList (FirstName, LastName, eMailAddress, ZipCode) VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[email]', $_POST[zipcode])";
                mysql_query($insertSQL, $con);
                echo "Thank you for subscribing to our mailing list, " . $_POST[fname] . "!";
            }
            else { echo $_POST[email] . " is already subscribed to our mailing list."; }
        }
    ?>

    <strong>Please fill out the form below to subscribe to someone's electronic mailing list!</strong><br />
  
    <form action="index.php" method="post" name="mailForm">
        <table>
            <tr><td align="right">First Name:</td> <td><input type="text" name="fname" maxlength="10" /></td></tr>
            <tr><td align="right">Last Name:</td> <td><input type="text" name="lname" maxlength="10" /></td></tr>
            <tr><td align="right">E-mail Address:</td> <td><input type="text" name="email" maxlength="30" /></td></tr>
            <tr><td align="right">ZIP Code:</td> <td><input type="text" name="zipcode" maxlength="5" /></td></tr>
        </table>
        <input type="submit"/>
    </form>
 
The first time that a person goes to the page, I don't want it to say "is already on our mailing list". I'm trying to use two IF statements instead of an IF/ELSE, but that's not working either...
This is simply a matter of logic. It won't work as you imagine it should because you are telling it to say "already subscribed" if the conditional statement is not true. At first load, the conditional is not true, therefore the "already subscribed" statement will appear. It is working exactly as programmed.

If you want to fix the behavior, you should do as GaryJohnson suggested. His suggestion shows that you should put your PHP code inside an if block that checks if $_POST['email'] is set in the first place. This is a very good way to go about it, so that no code is executed if no submission has happened in the first place. In fact, even if you mix your PHP and HTML code, it is most likely in your best interest to make sure that a submission has occurred before any post-submit code (especially code that makes any sort of query) is executed so that no unnecessary processing happens.

Also, as a general guide, if you have three cases to deal with (in your case, "New Email, accept", "Duplicate Email, warn", and "First Load, do nothing"), then naturally the decision structure cannot be just two branches (in your case, if-else, or, as you said you've tried, if-if). It has to be three branches, which would be if-elseif-else. Or, in the solution of GaryJohnson, an if-else inside an if block.
 
Thanks so much for the clarifications guys...whew. Pretty newb shit here, I know, but really appreicate the help. Works great!
 
Back
Top