Neil P
Neil P

Reputation: 40

PHP script - echoing POSTed variables - Adding info to DB

The php code on the web page calls out to a script in another file I can't even get it to echo out the variables I'm handing to it. I admit I'm pretty new to this, and am probably missing something obvious here.

                <p>
                <? if($_GET['e'] == "good"){ ?>
                    <h1><font color="#000000">Thank You! We will reply back as soon as possible.</font></h1><br /><br />
                <? } ?>

                <? if($_GET['e'] == "email"){ ?>
                    <h1><font color="#FF0000">Please check your input for bad email address or missing info!</font></h1><br /><br />
                <? } ?>

                <? if($_GET['e'] == "limit"){ ?>
                    <h1><font color="#FF0000">You have already been added!</font></h1><br />Feel Free to Call us @ !<br />
                <? } else { ?>
                    <h2>Input your info here:</h2>

                        <div style="height:20px"></div>
                        <form method="POST" action="doaction.php">
                        <table>
                            <tr><td width="60px">
                                <h3>Prefix:</h3></td><td width="260px">
                                <select name="prefix">
                                <option value="Mr.">Mr.</option>
                                <option value="Mr.">Mrs.</option>
                                <option value="Mr.">Ms.</option>
                                </select>
                            </td></tr>
                            <tr><td width="60px">
                                <h3>First Name:</h3></td>   <td><input class="short_txt2" name="fname" type="text" />
                            </td></tr>
                            <tr><td width="60px">
                                <h3>Last Name:</h3></td>    <td><input class="short_txt2" name="lname" type="text" />
                            </td></tr>
                            <tr><td>
                                <h3>E-mail:</h3></td>       <td><input class="short_txt2" name="email" type="text" />
                            </td></tr>
                        </table>

                        <div style="height:17px;">
                            <input type="hidden" name="do" value="addNLU">
                            <input class="submit2" name="send" type="submit" value="Join" />
                            <input class="submit2" name="reset" type="submit" value="Reset" />
                        </div>

                        </form>
                <? } ?>
            </p>

Here's the script it's supposed to run (again. it won't even echo the variables on the first 4 lines...) (Now corrected to have the variables pulled from the POST - still nothing on the return script):

<?
session_start();
include "_funcs.php"; //This is where checkEmail is

$dothis = $_GET['do'];
if(empty($dothis)) $dothis=$_POST['do'];


if($dothis == "addNLU"){
$_SESSION['Newsletter'] = $_SESSION['Newsletter'] + 1;
$prefix = $_POST['prefix'];
$fname = strip_tags($_POST['fname']);
$lname = strip_tags($_POST['lname']);
$name = $prefix." ".$fname." ".$lname;
$email = strip_tags($_POST['email']);
$time = time();
echo $prefix;
echo $fname;
echo $lname;
echo $email;
exit;
$okemail = checkEmail($email);
if($okemail == 1){
    //if($_SESSION['Newsletter'] <= 2){  //INSTEAD OF THIS - CHECK THE DB TO SEE IF THEY'RE ALREADY THERE (NOT HOW MANY TIMES THEY HAVE SUBMITTED...)
        If !empty ($name) && !empty($email) {
            include "_db.connect.php";
            $sql=mysql_query("INSERT INTO `zombiete`.`newsletter` (`prefix`, `lname`, `fname`, `email`, `when`) VALUES ('$prefix', '$lname', '$fname', '$email', '$time')")";
            mysql_close($db);
            $subject = "Newsletter user!";
            $body = "A new Newsletter User has been added: \n\n".$name."\n\nTake a Look!.\n\nhttp://www.us.us/workorder/newsletterlist.php";
            mail("[email protected]", $subject, $body, "From: us.us");
            $subject = "Thanks for requesting our newsletter!";
            $body = "Hello ".$name."!\n\nYtour name and email address has been added to our database, and one of our newsletters should be arriving soon.\n\nThanks again, and be sure to let us know if there's anything we can do for you.";
            mail($email, $subject, $body, "From: [email protected]");
            header("Location: newsletter.php?e=good");
            exit;
        } else {
            header("Location: newsletter.php?e=email");
            exit;
        }
    //} else {
    //  header("Location: newsletter.php?e=limit");
    //}
} else {
    header("Location: newsletter.php?e=email");
}
}

?>

(Some info in the doaction script has been changed to protect certain variables from disclosure. Please disregard generic database name. email address to send to, I have those in there correctly.)

Upvotes: 0

Views: 70

Answers (4)

Shehary
Shehary

Reputation: 9992

Try this, hope this helps

<?php session_start();
include "_funcs.php"; //This is where checkEmail is
include "_db.connect.php"; //This is database

//$dothis = $_GET['do']; Don't need this in my opinion
//if(empty($dothis)) $dothis=$_POST['do']; Don't need this in my opinion

if(isset($_POST['do']) && $_POST['do']=="addNLU"){
    $fname = mysql_real_escape_string($_POST['fname']);
    $lname = mysql_real_escape_string($_POST['lname']);
    $email = mysql_real_escape_string($_POST['email']);
    $prefix = mysql_real_escape_string($_POST['prefix']);   
    $name = $prefix." ".$fname." ".$lname;
    echo $prefix;
    echo $fname;
    echo $lname;
    echo $email;

    $newsletter = $_SESSION['Newsletter'];

    //exit; // Why you wana exit here???
    $okemail = checkEmail($email);

    if(!empty($okemail)){
        //CHECK THE DB TO SEE IF THEY'RE ALREADY THERE (NOT HOW MANY TIMES THEY HAVE SUBMITTED...)
        //run your query here
        //set your conditions, header location, exit
        header("Location: newsletter.php?e=limit");
        exit();
    } else If (!empty ($name) && ($email)) {         
            $sql=mysql_query("INSERT INTO `zombiete`.`newsletter` (`prefix`, `lname`, `fname`, `email`, `when`) VALUES ('$prefix', '$lname', '$fname', '$email', '$time')")";
            mysql_close($db);
            //Here you can put a check that above query run successful if you like before sending email 
            $subject = "Newsletter user!";
            $body = "A new Newsletter User has been added: \n\n".$name."\n\nTake a Look!.\n\nhttp://www.us.us/workorder/newsletterlist.php";
            mail("[email protected]", $subject, $body, "From: us.us");

            $subject = "Thanks for requesting our newsletter!";
            $body = "Hello ".$name."!\n\nYtour name and email address has been added to our database, and one of our newsletters should be arriving soon.\n\nThanks again, and be sure to let us know if there's anything we can do for you.";
            mail($email, $subject, $body, "From: [email protected]");
            header("Location: newsletter.php?e=good");
            exit;
    } else {
        header("Location: newsletter.php?e=email");
        exit;
    }
}
?>

Upvotes: 0

Abey
Abey

Reputation: 1428

Instead of,

if($dothis == "addNLU")

you should use,

if($dothis == $_POST['addNLU']){

Same goes for prefix, fname etc. As you are POSTing it from a form you should use $_POST['fname'] to access them.

Also a syntax error here If !empty ($name) && !empty($email). It should be If (!empty ($name) && !empty($email)).

Upvotes: 0

Sky
Sky

Reputation: 4370

Just change <? to <?php. <? is wrong. Your code should look like this:

<?php
//...
?>

Upvotes: 0

devnull
devnull

Reputation: 608

The values are stored in the $_POST superglobal.

first name can be retrieved using

$_POST['fname']

but not only

$fname

unless you specifically assign a value to $fname

$fname = $_POST['fname'];

You need to do this BEFORE echoing $fname.

Upvotes: 1

Related Questions