Jump to content

Making a contact form


Recommended Posts

Hey there. I'm trying to build a contact form from scratch (using the W3Schools tutorials pretty heavily), but I'm having some issues. First off, I'd like to write in a bit of code that grabs the username of the person submitting the form, and sends it along with the other submitted information. Also, if the person isn't signed in, they won't be able to use the form. I'm sure this will involve some PHP, which I will be more than happy to do, if someone could perhaps point me in the right direction?

<div id="events"><form name="event-submit" action="MAILTO:someone@example.com" method="post" enctype="text/plain">Event Name:<br /> <input type="text" name="eventname" /><br />Start Date:<br /> <input type="text" name="startdate" /><br />End Date:<br /> <input type="text" name="enddate" /><br />Location:<br /> <input type="text" name="location" /><br />Description:<br /> <input type="text" name="description" size="50" /><br /><input type="submit" value="Submit" /></form></div>

(And yes, I do have my actual email address for the MAILTO property, I just omitted it for this post.) My main problem right now is, the submit button isn't working. Buttons and I do not get along, it seems...Any suggestions as to what I'm doing wrong would be greatly appreciated. EDIT Okay, I apparently left off a rather important caret. The button is working just fine now, but is there any way to make the email not go through Outlook, and go directly to the email address?

Edited by tnd1000
Link to post
Share on other sites

I'm not sure where in the tutorials you got that the action should be a mailto. The point of forms is that they get processed server side. mailto to is an HTML implementation, but is not well supported. So first, try following some of the tutorials to make sure you can make a form and have it submit and output the results for you to see.http://www.w3schools.com/php/php_forms.asphttp://www.w3schools.com/php/php_get.asphttp://www.w3schools.com/php/php_post.asp Then you can try integrating that step into sending an emailhttp://www.w3schools.com/php/php_mail.asphttp://www.w3schools.com/php/php_secure_mail.asp Once you've got that, you can then go to the next step and try adding some sort of authentication, in this case using a logged in user. For that you will probably need to learn about $_SESSION and some SQL, as the most common conventions involve a users table for handling logins. But that should come after you get and understand the basics.

  • Like 1
Link to post
Share on other sites
<?phpfunction spamcheck($field)  {  //filter_var() sanitizes the e-mail  //address using FILTER_SANITIZE_EMAIL  $field=filter_var($field, FILTER_SANITIZE_EMAIL);  //filter_var() validates the e-mail  //address using FILTER_VALIDATE_EMAIL  if(filter_var($field, FILTER_VALIDATE_EMAIL))	{	return TRUE;	}  else	{	return FALSE;	}  }if (isset($_REQUEST['email']))  {//if "email" is filled out, proceed  //check if the email address is invalid  $mailcheck = spamcheck($_REQUEST['email']);  if ($mailcheck==FALSE)	{	echo "Invalid input";	}  else	{//send email	$email = $_REQUEST['email'] ;	$eventname = $_REQUEST['eventname'] ;	$startdate = $_REQUEST['startdate'] ;	$enddate = $_REQUEST['enddate'] ;	$location = $_REQUEST['location'] ;	$details = $_REQUEST['details'] ;	mail("someone@example.com", "Event Name: $eventname",	$details, "From: $email" );	echo "Thank you for submitting your event";	}  }else  {//if "email" is not filled out, display the form  echo "<form method='post' action='mailform.php'>  Email: <input name='email' type='text' /><br />  Event Name: <input name='eventname' type='text' /><br />  Start Date: <input name='startdate' type='text' /><br />  End Date: <input name='enddate' type='text' /><br />  Location: <input name='location' type='text' /><br />  Details:<br />  <textarea name='details' rows='15' cols='40'>  </textarea><br />  <input type='submit' />  </form>";}?>

Is there something I'm still doing wrong here? My button does nothing at all, and for some reason all of the symbols after </form> are rejected from the code and show up on the site as characters.

Link to post
Share on other sites

Give the submit button a name: <input type='submit' name="submit" /> Then here, for the if (isset($_REQUEST['email'])) , you can do this: if (isset($_POST['submit'])) If you want to make it 'easier', instead of the way you have the form being echoed, you can do this:

else  { //if "email" is not filled out, display the form?>  <form method="post" action="mailform.php">  Email: <input name="email" type="text" /><br />  Event Name: <input name="eventname" type="text" /><br />  Start Date: <input name="startdate" type="text" /><br />  End Date: <input name="enddate" type="text" /><br />  Location: <input name="location" type="text" /><br />  Details:<br />  <textarea name="details" rows="15" cols="40">  </textarea><br />  <input type="submit" name="submit" />  </form><?php}?>

Edited by Don E
Link to post
Share on other sites

what doesn't work? what's your updated code? Please be specific when looking for feedback. What is it doing? What isn't it doing? What do you want it to be doing? Are you debugging to test parts of your code to make sure values are what you expect them to be? Have you done the bare minimum and been able to echo all your form values? Have you verified that part of it yet? You should be testing incrementally before jumping ahead, as your experience requires that you understand everything that your code is doing, line by line, before you use it.

Edited by thescientist
Link to post
Share on other sites

The submit button still does nothing when I click on it. I may need to do some work on the site itself to incorporate the form into the software. Apologies. I have not debugged any of it yet, as I am only able to work with the program Notepad. I'm unable to download any programs with my current internet connection (dial-up), so I'll see if I can find an online debugger. I'll work with it until I figure something out. Thank you both for your help.

Link to post
Share on other sites

It may be because of here: $email = $_REQUEST['email'] ; $eventname = $_REQUEST['eventname'] ; $startdate = $_REQUEST['startdate'] ; $enddate = $_REQUEST['enddate'] ; $location = $_REQUEST['location'] ; $details = $_REQUEST['details'] ; mail("someone@example.com", "Event Name: $eventname", $details, "From: $email" ); echo "Thank you for submitting your event"; Try changing $_REQUEST to $_POST for the above since the form above is using method="post". $_REQUEST usually contains the contents of $_GET, $_POST and $_COOKIE in one global array($_REQUEST). Usually $_POST helps narrowing it down.Make sure 'someone@example.com' is an actual working email address. Are you using WAMP or XAMP or are you doing this from the Social program? I suggest you get yourself a nice editing program like Notepad++, or netbeans, etc(both free, when you're on a faster connection). Dreamweaver is nice if you have that kind of money but I highly suggest only working in the 'code view' side(there's a trial version). When debugging, what scientist meant by testing part of your code, you test a single part of your code making sure everything works for that specific part, then move on to the next part, etc.. This is referred to 'unit testing'.

  • Like 1
Link to post
Share on other sites

debugging can be as simple as having this at the top of mailform.php to start and make sure at least this works before adding the rest.

<?phpecho 'start of mailform.php<br>';if(isset($_POST['submit'])){  echo 'form submitted.  view POST<br>';  var_dump($_POST);};?>

there's nothing special you have to do, or software to get, except start from the ground up and make sure each little bit works. make sure you get all the $_POST variables to show first. nothing will work as expected without them.

Edited by thescientist
  • Like 1
Link to post
Share on other sites

did you change

action="MAILTO:someone@example.com

to

action="proccessscript.php"

? (where proccessscript.php is the file that contains the script you posted above. You could also submit the page to itself but make sure you change it's extension to .php )

Edited by TheGallery
  • Like 1
Link to post
Share on other sites

@Don: Okay, thanks. I'll definitely try changing those to $_POST. And yes, I'm using a working email address that I created through my hosting company's control panel. @scientist: Ahh, I see. Yeah, that's generally a good way to go about it. Thank you for clarifying. @Gallery: Yep, I took out the MAILTO function, but I did want to clarify something: I'll have to first create the .php file that I call to in the 'action' spot, right? The only reason why I ask instead of just doing it, is because the software I'm using is iffy, and their customer support team won't help me if I mess up the software. I'll definitely do some testing to see where all I went wrong with this thing.

Link to post
Share on other sites

what software? I don't see anything in there other just normal PHP. You can create either one first, just make sure the names of the inputs in the HTML are the same members you look for in $_POST in the script

  • Like 1
Link to post
Share on other sites

Ahh, thank you very much. I think it's actually starting to function correctly now. And I'm using SocialEngine software. The way it's set up, there are a lot of files in my public_html directory. Just wanted to make sure I was supposed to actually create the file before I went around adding things to their setup. EDIT Please excuse me for shouting, but...IT WORKED!!! Many thanks to all three of you for basically walking me through this (I'm not used to things working properly, especially when I'm involved).

Edited by tnd1000
  • Like 3
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...