Forum Moderators: coopster

Message Too Old, No Replies

my <?php echo "$display block"; ?> won't display

everything else shows up except this ...

         

icorange

2:51 am on Apr 30, 2008 (gmt 0)

10+ Year Member



Hi!

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]

wheelie34

10:08 am on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try without the quotes

<?php echo $display_block; ?>

icorange

12:20 pm on Apr 30, 2008 (gmt 0)

10+ Year Member



Thanks ... removed the quotes <?php echo $display_block; ?> ... still no change ... could it have anything to do with what files I bring in to my index.php page? I have it at the very top as:

<?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

RonPK

1:08 pm on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (!$_POST[op] !="ds") {

Maybe one of the exclamation marks is a typo?

henry0

1:47 pm on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



since the form goes to SELF
and you use three times $display_block
could it be as simple as renaming the #2 and #3
$display_block2 and $display_block3

icorange

1:48 pm on Apr 30, 2008 (gmt 0)

10+ Year Member



yay! that did it! Thank you very much ... but now ... when I enter an email name into the form and click 'subscribe' then 'Submit' NOTHING happens ... any ideas where I should start to look for this error?

Your help is greatly appreciated!

icorange

2:04 pm on Apr 30, 2008 (gmt 0)

10+ Year Member



sorry ... removing the extra exclamation made the form display ...

henry0

2:08 pm on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad I was of some help :)

bubbasheeko

4:53 am on May 2, 2008 (gmt 0)

10+ Year Member



Hi icorange,

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.

icorange

6:02 pm on May 2, 2008 (gmt 0)

10+ Year Member



Thank you Bubbasheeko ... you are exactly right! It was missing - I fixed it last night and thanks to everyone (and a couple of post) I got the page working! I'm thrilled ... thank you thank you thank you!