Jump to content


Photo

Form in PHP: problem getting submitted vars


  • Please log in to reply
3 replies to this topic

#1 nearlyalaugh

nearlyalaugh
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 May 2006 - 08:40 PM

Wrote a simple 'email this link' script a while back that worked perfectly, & decided--after having done all that work--that i'd turn my old perl email form to php, seeing as i "knew how things work." well, i've got what looks like a simple form, and one that looks like it should work... but does not.

the form action is itself. there are four required fields: name, from, subject, and message. if all are defined, it is supposed to conduct a mail() function and send it off to me and echo a thank you; else it's to display the form. instead of doing that, however, when submitted it indeed shows up in the url bar (method=get), but it returns the form, empty--can't even echo the vars.

the code is below, can anyone tell what's wrong?

<?php
define("title","nearlyalaugh.com - Contact Me");

include("include1.php"); // THIS IS JUST THE GENERIC STUFF, SHOULDN'T AFFECT THE FORM

$message = stripslashes($message);
$to      = "myemail@nearlyalaugh.com";
$header  = "From: $name <$from>";

// IF ALL VARS SET, EXECUTE MAIL
if ($name and $from and $subject and $message and $submitted) {
   mail($to,$subject,$message,$header);
   echo "
      <h2>Thank You for Contacting Me!</h2>
      <div class=\"desc\">I'll get on it soon as I can</div>
      <p>
         <b>What You Submitted:</b><br />
         Name: $name<br />
         Email: $from<br />
         Subject: $subject<br />
         Message: $message
      </p>
   ";
}

// OTHERWISE
else {
   echo "
      <h2>Comments, Questions? Contact Me!</h2>
      <div class=\"desc\">Just fill out the form below. All fields are <span class=\"highlight\">required</span>.</div>
      <p>
   ";

   // IF SUBMITTED WITHOUT ALL INFO
   if ($submitted) {
      if (($name == "") or ($from == "") or ($subject == "") or ($message == "")) {
         echo "All fields are required. You forgot:";
         if ($name == "") { echo " - <b>Your Name</b>"; }
         if ($from == "") { echo " - <b>Your Email</b>"; }
         if ($subject == "") { echo " - <b>Your Subject</b>"; }
         if ($message == "") { echo " - <b>Your Message</b>"; }
      }
   }

   // HERE'S THE FORM
   echo "
      <form action=\"$_SERVER[PHP_SELF]\" method=\"get\">
      <input type=\"hidden\" name=\"submitted\" value=\"true\" />

      <b>Your Name:</b><br />
      <input type=\"text\" name=\"name\" value=\"$name\" style=\"width:50%;\" /><br />

      <b>Your Email:</b><br />
      <input type=\"text\" name=\"from\" value=\"$from\" style=\"width:50%;\" /><br />

      <b>Subject:</b><br />
      <input type=\"text\" name=\"subject\" value=\"$subject\" style=\"width:50%;\" /><br />

      <b>Your Message:</b><br />
      <textarea name=\"message\" rows=\"10\" cols=\"60\" wrap=\"virtual\">$message</textarea><br />

      <input type=\"reset\" value=\"Clear All\" onclick=\"return confirm('Clear All Fields?!')\" />
      <input type=\"submit\" value=\"Send Email\" />
      </form>
      </p>
   ";
}

include("include2.php"); // THE GENERIC FOOTER

?>
any help would be greatly appreciated, thanks!

#2 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 07 May 2006 - 09:08 PM

register globlas is probably off, as it should be, so your variables need to be $name = $_GET['name']; for example.
Personal site: NewEnigma  |  Java and JSF Discussion

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 07 May 2006 - 09:11 PM

Possibly, register_globals is set to OFF on your server (all current releases of php have that as the default setting - don't mess with it) and so you need to retrieve the passed variables from the $_GET array. For example:
$name = $_GET['name'];

too slow again :)
Legend has it that reading the manual never killed anyone.
My site

#4 nearlyalaugh

nearlyalaugh
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 May 2006 - 09:25 PM

thanks, that occurred to me... but i only halfheartedly tried it. now it works, for all those interested i indeed did need to add

$name    = $_GET['name'];
$from    = $_GET['from'];
$subject = $_GET['subject'];
$message = $_GET['message'];
$submitted = $_GET['submitted'];
it's curious though: in the mentioned email-this-link script and another test script, i didn't have to set all of em!

for instance, this works:
<?php

echo "
<p>
<form action=\"$_SERVER[PHP_SELF]\" method=\"get\">
<input type=\"text\" name=\"tinput\" value=\"$tinput\" />
<input type=\"hidden\" name=\"submitted\" value=\"true\" />
<input type=\"submit\" value=\"submit\" />
</form>
</p>
";

echo "
<p>
Submitted: <b>$submitted</b><br />
You Wrote: <b>$tinput</b>
</p>
";

if ($submitted and $tinput) { echo "you submitted stuff!"; }
elseif ($submitted) { echo "you pressed submit!"; }
else { echo "run for your life!"; }

?>
so there's probably something deeper going on, but what i did works and it's a better way to go anyways

thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users