August 14, 201114 yr Hi! I have a contactform validated php... When you type in letters in the phone field there is an error that tells you that you are only allowed to use numbers. To my problem... when this error end the other arises they are in times new roman and behind the form.. how can i get these in a pop up or something like that!? Help is appreciated!
August 14, 201114 yr Javascript alert should do it http://www.w3schools.com/js/js_popup.asp Use something like: echo "<script type=\"text/javascript\">alert('You can only use numbers');</script>";
August 15, 201114 yr Author Javascript alert should do it http://www.w3schools.com/js/js_popup.asp Use something like: echo "<script type=\"text/javascript\">alert('You can only use numbers');</script>"; this is my code: <?php if(isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){ $errors = array(); $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $message = $_POST['message']; if(empty($name)){ $errors[] = "Ad your name!"; } if(empty($phone)){ $errors[] = "Fill your number!"; } if(!is_numeric($phone)){ $errors[] = " value="Only numbers please!"; } if(empty($email)){ $errors[] = "Fill a valid Email"; } if(empty($message)){ $errors[] = "Please type a message!"; } if(!empty($errors)){ foreach($errors as $error){ echo $error, "<br />"; } } else{ ini_set("SMTP", "smtp.trondan.se"); $to = "trondan@live.se"; $subject = "From trondan"; $headers = "From: ".$email; $body = "Email From:$name -- Number: $phone -- Email: $email -- Message: $message"; mail($to, $subject, $body, $headers); echo "Message sent!"; } } ?> <html> <head> <link href="http://trondan.se/erik/form.css" media="all" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function restoreValue(obj){ if(obj.value == ""){ obj.value = obj.defaultValue; } } function clearValue(obj){ if(obj.value == obj.defaultValue){ obj.value = ""; } } </script> </head> <body> <form action="" method="POST"> <font style="font-size:13px" color="#C0C0C0" face="Arial"> <input type="text" name="name" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:15px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:2" name="Name" value="Name"> <input type="email" name="email" onfocus="clearValue(this);" onblur="restoreValue(this);"style="position:absolute;left:79px;top:59px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:4" name="email" value="Email"> <input type="text" name="phone" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:103px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:7" name="phone" value="Phone"> <textarea name="message" id="Text" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:80px;top:147px;width:350px;height:270px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:6" rows="13" cols="31">Message</textarea> <input id="submit" type="submit" value="Contact me" style="position:absolute;left:310px;top:425px;width:120px;height:40px;z-index:23"> </font> </form> </body> </html> And you see this: $errors = array(); $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $message = $_POST['message']; if(empty($name)){ $errors[] = "Ad your name!"; } if(empty($phone)){ $errors[] = "Fill your number!"; } if(!is_numeric($phone)){ $errors[] = " value="Only numbers please!"; } if(empty($email)){ $errors[] = "Fill a valid Email"; } if(empty($message)){ $errors[] = "Please type a message!"; } Those alerts are the one I want to be in a pop up... can I use that code for that?
August 15, 201114 yr What's this line all about? if(!is_numeric($phone)){ $errors[] = " value="Only numbers please!"; }
August 15, 201114 yr though i am still learning ,i guess u need to use php pregmatch for that if i am wrong anyone can correct me pregmatch tutorial Edited August 15, 201114 yr by sash_oo7
August 15, 201114 yr To put your errors into a pop up you would need to use if(!empty($errors)) { // JavaScript popup echo "<script type=\"text/javascript\">alert("; foreach($errors as $error){ echo "'".$error."' + '\\n' + "; } echo "'');</script>"; // NoScript, in case javascript is disabled echo "<noscript>"; foreach($errors as $error){ echo $error, "<br />"; } echo "</noscript>"; }
August 15, 201114 yr Author To put your errors into a pop up you would need to use if(!empty($errors)) { // JavaScript popup echo "<script type=\"text/javascript\">alert("; foreach($errors as $error){ echo "'".$error."' + '\\n' + "; } echo "'');</script>"; // NoScript, in case javascript is disabled echo "<noscript>"; foreach($errors as $error){ echo $error, "<br />"; } echo "</noscript>"; } Where would i put that? Pretty new with php..
August 15, 201114 yr To put your errors into a pop up you would need to use if(!empty($errors)) { // JavaScript popup echo "<script type=\"text/javascript\">alert("; foreach($errors as $error){ echo "'".$error."' + '\\n' + "; } echo "'');</script>"; // NoScript, in case javascript is disabled echo "<noscript>"; foreach($errors as $error){ echo $error, "<br />"; } echo "</noscript>"; } Just as a side note. No offence but this is a mess, I would never dream of outputting javascript like this and I'm sure advanced developers will tell u the same thing.
August 15, 201114 yr Author Just as a side note. No offence but this is a mess, I would never dream of outputting javascript like this and I'm sure advanced developers will tell u the same thing. What to do then?
August 15, 201114 yr Looking back at the code, it does look messy and there are better ways of doing it, but they would require a lot more modification to the code, this is just a quick short term solution I made in a short amount of time. This is how it would be implemented into your code – <?php if(isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){ $errors = array(); $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $message = $_POST['message']; if(empty($name)){ $errors[] = "Ad your name!"; } if(empty($phone)){ $errors[] = "Fill your number!"; } if(!is_numeric($phone)){ $errors[] = "Only numbers please!"; } if(empty($email)){ $errors[] = "Fill a valid Email"; } if(empty($message)){ $errors[] = "Please type a message!"; } if(!empty($errors)) { // JavaScript popup echo "<script type=\"text/javascript\">alert('"; foreach($errors as $error){ echo $error."\\n"; } echo "');</script>"; // NoScript echo "<noscript>"; foreach($errors as $error){ echo $error, "<br />"; } echo "</noscript>"; } } else{ ini_set("SMTP", "smtp.trondan.se"); $to = "trondan@live.se"; $subject = "From trondan"; $headers = "From: ".$email; $body = "Email From:$name -- Number: $phone -- Email: $email -- Message: $message"; mail($to, $subject, $body, $headers); echo "Message sent!"; } } ?> <html> <head> <link href="http://trondan.se/erik/form.css" media="all" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function restoreValue(obj){ if(obj.value == ""){ obj.value = obj.defaultValue; } } function clearValue(obj){ if(obj.value == obj.defaultValue){ obj.value = ""; } } </script> </head> <body> <form action="" method="POST"> <font style="font-size:13px" color="#C0C0C0" face="Arial"> <input type="text" name="name" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:15px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:2" name="Name" value="Name"> <input type="email" name="email" onfocus="clearValue(this);" onblur="restoreValue(this);"style="position:absolute;left:79px;top:59px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:4" name="email" value="Email"> <input type="text" name="phone" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:79px;top:103px;width:351px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:7" name="phone" value="Phone"> <textarea name="message" id="Text" onfocus="clearValue(this);" onblur="restoreValue(this);" style="position:absolute;left:80px;top:147px;width:350px;height:270px;color:#C0C0C0;font-family:Arial;font-size:13px;z-index:6" rows="13" cols="31">Message</textarea> <input id="submit" type="submit" value="Contact me" style="position:absolute;left:310px;top:425px;width:120px;height:40px;z-index:23"> </font> </form> </body> </html>
August 16, 201114 yr Author That does not seem to work.. Parse error: syntax error, unexpected T_STRING in /storage/content/trondan.se/public_html/test.php on line 16
August 16, 201114 yr If it's that line on your original code, it's because you are using ' " ' inside your error, when you are using the same thing to define its contents Change $errors[] = " value="Only numbers please!"; To something like $errors[] = "Only numbers please!"; If it's not that you've probably missed out the semicolon at the end, or you're missing a '{' or '}' somewhere
Create an account or sign in to comment