-1

I have problem with pop up form . It doesn't send email. Here is html form:

<form action="#" method="post" id="form" >
  <img src="images/3.png" id="close"/>
  <h2>Contact Us</h2><hr/>
  <input type="text" name="name" id="name" placeholder="Name"/>
  <input type="text" name="email" id="email" placeholder="Email"/>
  <textarea name="message" placeholder="Message" id="msg"></textarea>
  <a id="submit" href="javascript: check_empty()">Send</a>
</form>

JS to pop up html form:

function check_empty(){
if(document.getElementById('name').value == "" 
|| document.getElementById('email').value == "" 
||document.getElementById('msg').value == "" ){
alert ("Fill All Fields !");
}
    else {  
    document.getElementById('form').submit();  
    alert ("Form submitted successfully...");
    }
}

//function to display Popup
function div_show(){ 
document.getElementById('abc').style.display = "block";
}

//function to check target element
function check(e){ 
var target = (e && e.target) || (event && event.srcElement); 

var obj = document.getElementById('abc'); 
var obj2 = document.getElementById('popup'); 

checkParent(target)?obj.style.display='none':null; 
target==obj2?obj.style.display='block':null; 

} 

//function to check parent node and return result accordingly
function checkParent(t){ 
    while(t.parentNode){ 
        if(t==document.getElementById('abc'))
            { 
                return false 
            }
        else if(t==document.getElementById('close'))
            {
                return true
            } 
        t=t.parentNode 
    } 
    return true 
} 

And php function to send form data to email. Everything work but i don't receive email on gmail. Similar php script i used to post email without pop up and it worked.

       <?php 
if(isset($_POST['submit'])){
    $to = "myemail@gmail.com"; 
    $from = $_POST['email']; 
    $first_name = $_POST['name'];
    $message = $first_name  . " wrote following:" . "\n\n" . $_POST['message'];   
    mail($to,$from,$message); 
    }
?>
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Fakt7
  • 187
  • 4
  • 15
  • Are you working on a server or localhost? – Charlotte Dunois Jul 31 '14 at 16:57
  • Your has `action="#"` so it won't go anywhere. It doesn't even go to the PHP script, let alone email. it. – putvande Jul 31 '14 at 16:57
  • Clue: Check your (PHP) conditional statement; it's dependant on it. – Funk Forty Niner Jul 31 '14 at 16:58
  • @CharlotteDunois on a server, another no pop up contact form work great. – Fakt7 Jul 31 '14 at 17:02
  • Just to clarify why `Send` doesn't work, is because it is not considered to be a valid POST element. Had you used `` it would have worked. @Fakt7 - PHP is looking for a named POST form "element". An `id` doesn't qualify as a "named" element; just so you know the differences. Which is why I said your PHP is dependant on the action. `if(isset($_POST['submit'])){...}` - That would've made your code work. – Funk Forty Niner Jul 31 '14 at 17:25

2 Answers2

1

Simple: You don't have an element named "submit" in your form, so your if() test always fails.
id != name in HTML forms; meaning, id does not equal name.

A simple work around:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... form was submitted ...
}

But this code is bad in any case. You should NEVER use only client-side validation. It's too easy to bypass. ALWAYS validate/verify on the server as well.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Just as my [clue](http://stackoverflow.com/questions/25064740/pop-up-form-does-not-send-email#comment38992440_25064740) states. Make them think about it first, then put 2 and 2 together. – Funk Forty Niner Jul 31 '14 at 16:58
  • But i have defined submit in html form `Send` And i am testing this submit in php. Is it bad? I tried to add name="submit" but it does not help – Fakt7 Jul 31 '14 at 17:06
  • with ` if ($_SERVER['REQUEST_METHOD'] == 'POST') {` it work. Interesting. Thanks – Fakt7 Jul 31 '14 at 17:12
  • 1
    @Fakt7 The "work around" works because of your form's method `method="post"` – Funk Forty Niner Jul 31 '14 at 17:16
  • @fakt: there's 3 things that can submit from a form: ``, ` – Marc B Jul 31 '14 at 20:35
0

You should set your action in the form to the URL of your action in the server.

jpbalarini
  • 1,082
  • 1
  • 17
  • 23