Forum Moderators: coopster
I have a simple email subscription form (form.php) that I require in my index.php page ... it worked before (when I used mysqli in the coding) I have changed it all to mysql because my host site does not yet support mysqli ... now everything is on my index.php page except when it's suppose to display the block in my form.php - nothing is there! I would REALLY appreciate some help ...
my form.php is as follows:
<?php
//set up a couple of functions
function doDB() {
global $conn;
//connect to server and select database; you may need it
$conn = mysql_connect('example.net', '#*$!#*$!xx', '#*$!#*$!x', '#*$!#*$!x')
or die(mysql_error());
mysql_select_db("subscription",$conn) or die(mysql_error());
}
function emailChecker($email) {
global $conn, $check_result;
//check that email is not already in list
$check = "SELECT id FROM SUBSCRIPTION WHERE email = '$email'";
$check_result = mysql_query($check,$conn) or die(mysql_error());
}
//determine if they need to see the form or not
if (!$_POST[op] !="ds") {
//they need to see the form, so create form block
$display_block = "
<form method=POST action=\"$_SERVER[PHP_SELF]\">
<p><strong>Your E-Mail Address:</strong><br>
<input type=text name=\"email\" size=40 maxlength=150>
<p><strong>Action:</strong><br>
<input type=radio name=\"action\" value=\"sub\" checked> subscribe
<input type=radio name=\"action\" value=\"unsub\"> unsubscribe
<p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
</form>";
} else if (($_POST[op] == "ds") && ($_POST[action] == "sub")) {
//trying to subscribe; validate email address
if ($_POST[email] == "") {
header("Location: form.php");
exit;
}
//connect to database
doDB();
//check that email is in list
emailChecker($_POST[email]);
//get number of results and do action
if (mysql_num_rows($check_results) < 1) {
//add record
$sql = "insert into subscription values('', '$_POST[email]')";
$result = mysql_query($sql,$conn) or die(mysql_error());
$display_block = "<p>Thanks for signing up!</p>";
} else {
//print failure message
$display_block = "<>You're already subscribed!</P>";
}
} else if (($_POST[op] == "ds") && ($_POST[action] == "unsub")) {
//trying to unsubscribe; validate email address
if ($_POST[email] == "") {
header("Location: form.php");
exit;
}
//connect to database
doDB();
//check that email is in list
emailChecker($_POST[email]);
//get number of results and do action
if (mysql_num_rows($check_result) < 1) {
//print failure message
$display_block = "<p>Couldn't find your address!</p>
<p>No action was taken.</p>";
} else {
//unsubscribe the address
$id = mysql_result($check_result, 0, "id");
$sql = "delete from subscription where id = '$id'";
$result = mysql_query($sql,$conn) or die(mysql_error());
$display_block = "<p>You're unsubscribed!</p>";
}
}
?>
The area in my index.php calling the above is as follows:
<?php echo "$display_block"; ?>
Thank you, thank you, thank you
[edited by: eelixduppy at 5:52 am (utc) on April 30, 2008]
[edit reason] exemplified [/edit]
<?php
require("form.php");
require("leftnav.php");
require("rightnav.php");
?>
However, my left and right navs show up ...
Any help would be greatly appreciated.
Thanks
Looking at the below..
//determine if they need to see the form or not
if (!$_POST[op] !="ds") {
//they need to see the form, so create form block
$display_block = "
<form method=POST action=\"$_SERVER[PHP_SELF]\">
<p><strong>Your E-Mail Address:</strong><br>
<input type=text name=\"email\" size=40 maxlength=150>
<p><strong>Action:</strong><br>
<input type=radio name=\"action\" value=\"sub\" checked> subscribe
<input type=radio name=\"action\" value=\"unsub\"> unsubscribe
<p><input type=submit name=\"submit\" value=\"Submit Form\"></p>
</form>";
Where is the field that is named 'op'? If it is not present it has nothing to test against for true or false. This will always result in false.
Why not change the switch to: if (!isset($_POST[action])? That would check to see if an 'action' has been set and if it hasn't proceed with the form.