Jump to content

Form in PHP: problem getting submitted vars


nearlyalaugh

Recommended Posts

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?

[code]<?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

?>[/code]
any help would be greatly appreciated, thanks!
Link to comment
Share on other sites

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:
[code]$name = $_GET['name'];[/code]

too slow again :)
Link to comment
Share on other sites

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

[code]$name    = $_GET['name'];
$from    = $_GET['from'];
$subject = $_GET['subject'];
$message = $_GET['message'];
$submitted = $_GET['submitted'];[/code]
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:
[code]<?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!"; }

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

thanks!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.