dazz_club Posted March 11, 2008 Share Posted March 11, 2008 Hi guys, On my site i have a input field for users to join my mail bag. So all it has is on field and a submit button. I trying to have the form and the script that submits the form on same page. I would like it this way because I would like to display a message, saying something like thank you, "you have been added" I had trouble submiting the data when it was on the form, so i created a seperate script. MY question is how much of the script would i need change to put it with the form page and for it to work?? Hope you understand Below is the script to submit the form data (well insert the data in my database) mailbag.php ------------ <?php $email=$_POST['email']; if (!$email) { echo 'You must enter data in all of the field'.'Plumb'; exit; } if (!get_magic_quotes_gpc()) { $email = addslashes($email); } @$db = new mysqli('localhost', 'root', 'xxx', 'xx'); if (mysqli_connect_errno()) { echo 'Error.'; exit; } $query = "INSERT INTO mail_bag (email_add) VALUES ('".$email."')"; $result = $db->query($query); if ($result) echo $db->affected_rows.'email inserted.'; $db->close(); ?> ------------ This is the page with the form, join-mailbag.php ---------------- <form method="post" action="<? echo $_SERVER['PHP_SELF'] ?>"> <ul id="mailBag"> <li><input type="text" name="email" class="mailBagTextBox" ></li> <li><input type="image" value="submit" src="images/site-images/mailbag-bttn.png" ></li> </ul> </form> --------------- currently the sate of play is that i have been attempting to combine the two on one page hence the action="<? echo $_SERVER['PHP_SELF'] ?> instead of action="mailbag.php" Kind regards Dazzclub Quote Link to comment Share on other sites More sharing options...
frijole Posted March 11, 2008 Share Posted March 11, 2008 i don't see why you can't put them together like they are. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 if ($_POST['email']) { // mail code goes here } else { // form goes here } Quote Link to comment Share on other sites More sharing options...
dazz_club Posted March 11, 2008 Author Share Posted March 11, 2008 Hi , should have thought of that really. Ok, after reading your guys postshave a merged them together and the code looks something like; <?php $email=$_POST['email']; if (!$email) { echo "<form method=\"post\" action=\"<? echo $_SERVER['PHP_SELF'] ?>\" >" ; echo "<ul id=\"mailBag\">"; echo "<li><input type=\"text\" name=\"email\" class=\"mailBagTextBox\" ></li>"; echo "<li><input type=\"image\" value=\"submit\" src=\"images/site-images/mailbag-bttn.png\" ></li>"; echo "</ul>"; echo "</form>"; exit; } if (!get_magic_quotes_gpc()) { $email = addslashes($email); } @$db = new mysqli('localhost', 'root', 'DARREN', 'tlc-line'); if (mysqli_connect_errno()) { echo 'Error.'; exit; } $query = "INSERT INTO mail_bag (email_add) VALUES ('".$email."')"; $result = $db->query($query); if ($result) echo $db->affected_rows.'email inserted.'; $db->close(); ?> When i go to try it out it displays; Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\wamp\www\tlc-line.com\includes\join-mailbag.php on line 7 I think i may know what the cause is to this, so i'll post back to see if i have corrected it. Thanks again for your replies and help. Kind regards Dazzclub Quote Link to comment Share on other sites More sharing options...
dotBz Posted March 11, 2008 Share Posted March 11, 2008 echo "<form method=\"post\" action=\"<? echo $_SERVER['PHP_SELF'] ?>\" >" ; i guess this should be echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">"; Quote Link to comment Share on other sites More sharing options...
dazz_club Posted March 11, 2008 Author Share Posted March 11, 2008 Thanks dotBz , ah man i cant believe i missed that, ive been looking at it for hours and didnt notice it. Cheers mate Quote Link to comment Share on other sites More sharing options...
dazz_club Posted March 12, 2008 Author Share Posted March 12, 2008 hi all, Just thought i'll post back and to say the script is now working Here is the script ---------------- <?php $email=$_POST['email']; if (!$email) { echo "<form method=\"post\" action=\"". $_SERVER['PHP_SELF'] . "\" >"; echo "<ul id=\"mailBag\">"; echo "<li><input type=\"text\" name=\"email\" class=\"mailBagTextBox\" ></li>"; echo "<li><input type=\"image\" value=\"submit\" src=\"images/site-images/mailbag-bttn.png\" ></li>"; echo "</ul>"; echo "</form>"; return; } if (!get_magic_quotes_gpc()) { $email = addslashes($email); } @$db = new mysqli('localhost', 'root', 'DARREN', 'tlc-line'); if (mysqli_connect_errno()) { echo 'Error.'; exit; } $query = "INSERT INTO mail_bag (email_add) VALUES ('".$email."')"; $result = $db->query($query); if ($result) echo "<div style=\"color:red;font-weight:bold;\">You have now been added. Thank you.</div>"; echo "<form method=\"post\" action=\"". $_SERVER['PHP_SELF'] . "\" >"; echo "<ul id=\"mailBag\">"; echo "<li><input type=\"text\" name=\"email\" class=\"mailBagTextBox\" ></li>"; echo "<li><input type=\"image\" value=\"submit\" src=\"images/site-images/mailbag-bttn.png\" ></li>"; echo "</ul>"; echo "</form>"; ?> ---------------- If anyone spots anything, like uneccessary code that would be great. Thanks for all your help. kind regards Dazzclub Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.