SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 25
-
Apr 3, 2009, 02:40 #1
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
help needed for form processing code
Hi all,
I am completely new with php and have been trying for over a month to add some simple form processing code to the website of a friend. I have tried different scripts and tried changing it several times. I have also previously had some help from people on this forum (and thank you very much I appreciate it) and nothing seems to work.
I am beginning to think that it must be the settings of the web hosting company but they say that it is the code and that they cant help with that as it is not their problem.
I am at my wits end and if anyone can help I would be eternally grateful.
Is anyone able to test to see if the code works as if they can I can contact the web hosting company and tell them it is a problem on their side.
Also is it best on the php script to start and finish it with the <head> and
<body> tags or does this make no difference?
When I load it to the web page and submit the info it always comes up with the message "No se ha realizado el envio de informaciones a $email" (which was originally "invalid email address but have had to change it as my friend is Spanish) and if I delete this bit of code, a blank screen pops up and the the info is not sent.
The php is:
Code:<?php /* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */ $email = 'jan_colombini@hotmail.com'; $contactname = $HTTP_POST_VARS['contactname']; $telephone = $HTTP_POST_VARS['telephone']; $email = $HTTP_POST_VARS['email']; $detalles = $HTTP_POST_VARS['detalles']; $tipo = $HTTP_POST_VARS['tipo']; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Correo Invalido</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif ($subject == "") { echo "<h4>Sin Mensaje</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */ elseif (mail($email, $contactname, $telephone, $email, $detalles, $tipo)) { echo "<h4>Su mensaje ha sido enviado</h4>"; } else { echo "<h4>No se ha realizado el envio de informaciones a $email</h4>"; } ?>
Code:<form action="form.php" method="post" class="contact"> <fieldset> <div> <label for="contactname" class="fixedwidth">Nombre y Apellido</label> <input type="text" name="contactname" id="contactname"/> </div> <div> <label for="telephone" class="fixedwidth">Numero de Telefono</label> <input type="text" name="telephone" id="telephone"/> </div> <div> <label for="email" class="fixedwidth">Correo Electronico</label> <input type="text" name="email" id="email"/> </div> <div> <label for="tipo" class="fixedwidth">Tipo de Seguro</label> <select name="tipo" id="tipo"> <option>Rodaje de spots</option> <option>Sesiones de Fotografía</option> <option>Cortometrajes</option> <option>Otros suguros</option> </select> </div> <div> <p>Texto</p> <div class="textarea"> <textarea name="detalles" id="details" cols="30" rows="7"></textarea> </div> <div class="buttonarea"> <input type="submit" value="Envianos la informacion"/> </div> </div> <p>Una vez recibida su consulta nos pondremos en contacto con usted en la mayor brevedad</p> </fieldset> </form>
Jan
-
Apr 3, 2009, 03:02 #2
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Hi Jan
There are a couple of problems in the form;
$HTTP_POST_VARS was removed along time ago! Use $_POST now
You check for the $subject field value but there is no subject field so the value will always be false
Try the following code:
PHP Code:$send = true;
$email = 'jan_colombini@hotmail.com';
$contactname = $_POST['contactname'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$detalles = $_POST['detalles'];
$tipo = $_POST['tipo'];
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Correo Invalido</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
$send = false;
}
/* THERE IS NO SUBJECT FIELD!!!
elseif ($subject == "") {
echo "<h4>Sin Mensaje</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
$send = false;
}*/
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
if($send != false) {
mail($email, $contactname, $telephone, $email, $detalles, $tipo);
echo "<h4>Su mensaje ha sido enviado</h4>";
} else {
echo "<h4>No se ha realizado el envio de informaciones a $email</h4>";
}
PHP Code:/** error handler
** set error reporting level to 0 for live site, 1 for dev */
ini_set("display_errors", 1);
ERROR_REPORTING(E_ALL);
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Apr 3, 2009, 07:07 #3
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Wow,
Spikez, I have had a lot of fantastic replies from this forum but this one really is on the top of the pile!!! Thank you very much. I cant test it at the moment as I am at work but I had a quick look at your reply and felt I had to send a thank you message to you for you hard work.
I will let you know over the weekend if it has worked or if I still have problems.
Thank you againJan
-
Apr 3, 2009, 07:56 #4
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
You are most welcome Jan, let me know how it goes
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Apr 3, 2009, 09:24 #5
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
One other thing to note: it looks like you're running a Regex over the $email variable to check to make sure that it's a valid email address.
There's actually a built in PHP function which will do this for you. Instead of running your Regex, try
PHP Code:$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
-
Apr 3, 2009, 10:30 #6
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Situationsoap,
Thanks for the advice, one question though, (I am still learning the basics I am afraid) do I replace the following code you suggested:
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
for the whole of the section that I had?
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Correo Invalido</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; $send = false;
or just the first line?
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email))
and if I have to change it all, what message will it display if an incorrect email is sent? (As I need it in Spanish)
Thanks again for your help and I will let you know how I get on.Jan
-
Apr 3, 2009, 11:14 #7
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Jan, the filter would only replace your preg. Sorry for not being clear on that one. I'd instead recommend
PHP Code:$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) or die("<h4>Correo Invalido</h4> <a href='javascript:history.back(1);'>Back</a>");
-
Apr 3, 2009, 11:19 #8
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Situationsoap,
I appreciate your help and will let you know how I get on.Jan
-
Apr 4, 2009, 08:10 #9
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
I am still having troubles, I amended the code to:
Code:<?php $send = true; $email = 'jan_colombini@hotmail.com'; $contactname = $_POST['contactname']; $telephone = $_POST['telephone']; $email = $_POST['email']; $detalles = $_POST['detalles']; $tipo = $_POST['tipo']; $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) or die("<h4>Correo Invalido</h4> <a href='javascript:history.back(1);'>Atras</a>"); echo "<a href='javascript:history.back(1);'>Atras</a>"; } elseif ($detalles == "") { echo "<h4>Sin Mensaje</h4>"; echo "<a href='javascript:history.back(1);'>Atras</a>"; } /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */ if($send != false) { mail($email, $contactname, $telephone, $email, $detalles, $tipo); echo "<h4>Su mensaje ha sido enviado</h4>"; } else { echo "<h4>No se ha realizado el envio de informaciones a $email</h4>"; } /** error handler ** set error reporting level to 0 for live site, 1 for dev */ ini_set("display_errors", 1); ERROR_REPORTING(E_ALL); ?>
I am not sure how to use the error handler bit so I put it at the bottom of the code but didn't seem to do anything.
Do you have any other suggestions? I hope I am not starting to be a pain in the a****.Jan
-
Apr 6, 2009, 05:54 #10
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You should post the error handling code at the top of your script. Didn't have a chance to look at anything else yet, though.
-
Apr 6, 2009, 06:24 #11
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No worries,
I will do that as soon as I can and let you know what comes upJan
-
Apr 6, 2009, 06:34 #12
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
After a second look, it seems that you're sending way too many parameters to the mail function. Google PHP mail for the manual page (I can't post links yet) for more details, but I think you'll need to combine your variables into one message before calling the function.
-
Apr 10, 2009, 06:50 #13
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have tried to put the error handling code at the top but it still shows a blank screen!
I have also changed the details to
}if ($detalles == "") {
Does anyone have any other suggestions?Jan
-
Apr 10, 2009, 07:04 #14
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A favorite method of mine it to use
PHP Code:print_r($_POST);
-
Apr 10, 2009, 07:41 #15
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Situationsoap,
You have worked so hard for me, I really appreciate it. Thank you.
I put the code at the top of the page and transferred the page to the website and... nothing it is still it is only showing a blank page. I must be doing something really wrong. Either that or it is a basic mistake!
I am not a quitter and I will get this sorted even if it kills me!! But in the mean time if you can think of anything else to help me I would really appreciate it!Jan
-
Apr 10, 2009, 07:52 #16
- Join Date
- Apr 2009
- Posts
- 115
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You are using $post values but is there even a form for the user to input these values? Can you post your entire file and give a link so we know if anything is outputted?
-
Apr 10, 2009, 08:01 #17
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi A2k,
My code now looks like this as I am no deleting any of the suggestions that I have found here:
Code:<?php print_r($_POST); /** error handler ** set error reporting level to 0 for live site, 1 for dev */ ini_set("display_errors", 1); ERROR_REPORTING(E_ALL); $send = true; $email = $_POST['email']; $contactname = $_POST['contactname']; $telephone = $_POST['telephone']; $email = $_POST['email']; $detalles = $_POST['detalles']; $tipo = $_POST['tipo']; $recipient = "jan_colombini@hotmail.com"; $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) or die("<h4>Correo Invalido</h4> <a href='javascript:history.back(1);'>Atras</a>"); echo "<a href='javascript:history.back(1);'>Atras</a>"; } elseif ($detalles == "") { echo "<h4>Sin Mensaje</h4>"; echo "<a href='javascript:history.back(1);'>Atras</a>"; } /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */ if($send != false) { mail($recipient, $email, $contactname, $telephone, $email, $detalles, $tipo); echo "<h4>Su mensaje ha sido enviado</h4>"; } else { echo "<h4>No se ha realizado el envio de informaciones a $email</h4>"; } ?>
If you can help i will be indebted to you!!
Thanks again.Jan
-
Apr 10, 2009, 08:22 #18
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You set a class value in your form declaration on the contact data entry page (the one you linked to above). Why did you do that? I'm not sure that class is a valid attribute for HTML Forms, and I think (?) that might be what's causing your problem.
-
Apr 10, 2009, 08:44 #19
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Really? I did not know that. I set it to be able to style it in CSS. You mean this bit right?
Code:<form action="form.php" method="post" class="contact">
Could you tell me if I understood right and the above line of code in my HTML is what you were referring to and could be causing the problem.Jan
-
Apr 10, 2009, 08:59 #20
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That is the line of code I was referring to. I can't promise I'm right: I'm not a CSS expert by any stretch.
Input types can most definitely have valid CSS definitions (and thus, classes), but Forms don't be default display anything: they simply collect and hold data, so I don't know why one would need a class for them.
Like I said, that just seemed odd to me, and I don't see anything else that's jumping out as incorrect right now. Let me know how that turns out.
-
Apr 10, 2009, 09:57 #21
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Forms are containers too. So if you want to style the area around a form (like box it), you'd be wasting code putting it inside a <div>, you can style the form as a block itself.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Apr 10, 2009, 10:41 #22
- Join Date
- Apr 2009
- Posts
- 248
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 10, 2009, 11:07 #23
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
You are willing to learn; therefore the word 'ignorance' is unjust.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Apr 10, 2009, 13:17 #24
- Join Date
- Feb 2008
- Location
- end($world)
- Posts
- 834
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Personally, I don't style forms with CSS anymore (well only the basic stuff like font, margins etc) because it is impossible to make them look the same in every popular browser (or at least in 4/5 most popular...).
I just leave the form elements appearance to the browser rendering engine. Saves tons of time and the forms, even though they look ugly by default in some browsers, at least match the browser interface
-
Apr 11, 2009, 03:19 #25
- Join Date
- Jan 2009
- Posts
- 58
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi risoknop,
I am only doing basic styling to the boxes themselves, Just to keep them in line and make them blend in a little, otherwise they seem to go all over the place.
I am not just new at PHP I am fairly new at web design in general so it is always great to have the opinions of more experienced designers.
I will have another look and play with the PHP for the form and maybe start a fresh and try one line at a time to see where the problem lies. I feel I have taken so much time from all the people here that I am starting to feel a little guilty.
Just wanted to say a HUGE thank you to everyone here as without Sitepoint and you guys I don't think I would have even able to build my first website!! Knowing I have such a great resource for help gives me a lot more confidence to try new things and lets me enjoy everything I do so much more.
Jan
Bookmarks