0

On clicking the submit button it is showing submit is not set after the if condition. I guess this message is showing because the form is not getting submitted. I don't know where I'm going wrong kindly help me fix this issue. I'm herewith attaching a part of my contact.php code and mail.php code. Thank you

contact.php

<form id="contact-form" method="POST" action="mail.php">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Name</label>
                                <input class="form-control" name="name" id="name" placeholder="" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Email</label>
                                <input class="form-control" name="email" id="email" placeholder="" type="email" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Subject</label>
                                <input class="form-control" name="subject" id="subject" placeholder="" required>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Message</label>
                        <textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
                    </div>
                    <div class="text-right"><br>
                    
                        
                        <input class="btn btn-primary solid blank button"  id="btn" type="submit" value="submit" name="submit">
                    
                    </div>
    
                </form>

mail.php

<?php
if (isset($_POST['email'])){
    
    echo "submit is set to {$_POST['submit']} and now we send the email<br>";

$to = "abc@email.com"; 
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";


if(mail ($to, $subject, $name,  $message)){
    echo "Enquiry sent successfully!<br>";
   
}
    else
    {
    echo "Mail was not sent. Please try again later<br>";
    }
} else{

echo"submit is not set<br>"; 
}

echo "after the if condition";

header('Location: https://imatrixautomation.com/contact.php');
exit;

?> 
  • 1
    Add this to the top of the code `ini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);' This will display errors. Check the browser console to see what is being submitted if you don't know. As a debugging it's good to do a var_dump on $_POST to see what is being sent. – Jason K Dec 30 '20 at 20:06
  • Simply change the method of the form to `method="GET"` and see the builded url on submit, u will find the data that is being sent to mail.php , On mail.php change all $_POST to $_GET and echo them one by one to debug. if everything looks good then change all methods back to post in both files and form too – stillKonfuzed Dec 30 '20 at 20:29
  • You need to check your html form, change the name of the input submit to email, because in your script you are looking for $_POST['email'] but in real its submit. – mrSotirow Dec 30 '20 at 21:18
  • @stillKonfuzed that's a very long and unnecessary way to debug. You could just look in the Network log of the browser's Developer Tools to get that information, without needing to change any code. Or use a tool like Fiddler. The web has existed for 30 years...did it not occur to you there might be a better way by now to see what a simple Post request is doing? Learn to use the tools available to you, it will save time – ADyson Dec 30 '20 at 23:49
  • @mrSotirow that's nonsense. There's a `name="email"` field there already in the form. There's no need to change the submit button. – ADyson Dec 30 '20 at 23:54
  • The code as shown should work. It's not clear how this is occurring, unless perhaps there is some JavaScript / AJAX involved which you haven't shown. – ADyson Dec 30 '20 at 23:58
  • there is no ajax /javascript involved and on debugging it showed heard missing so I added the hear location but still not working. I've edited the mail.php code kindly check –  Dec 31 '20 at 04:09
  • @ADyson do you think the user would post this issue if he knew about developer tools?? Teach them the simple solutiuon rather than suggesting them tools to debug which anyway would not make sense to them. GET method is the best way of debugging php code for newcomers as the data is displayed without any debugging tools. – stillKonfuzed Dec 31 '20 at 07:30
  • @stillKonfuzed it absolutely isn't the best way. You can make mistakes while changing the code back and forwards, so that's a risk. And it doesn't test the real code, but a changed version. And it takes a long time. And If the issue happened to be repeated specifically to that fact that _it was a POST request_ then your test would reveal nothing, or just add to the confusion. Don't teach people bad habits. They can learn developer tools in a few minutes, and there are tutorials available too. – ADyson Dec 31 '20 at 07:44
  • @Soumya adding the header will redirect back to the contact page but it doesn't solve the issue if the form isn't being processed or sent. Also, it should mean that you don't see any error or success messages, because you just get redirected instead. Can you confirm if that is now what is happening? "Doesn't work" is too vague...you need to describe the exact behaviour of the code. Also please use your browser's Developer Tools to see what is being submitted. And take the steps that Jason K outlined in order to enable error handling and output the POST array for debugging. – ADyson Dec 31 '20 at 08:01
  • @stillKonfuzed "noob"? Look at my [profile](https://stackoverflow.com/users/5947043/adyson?tab=profile). If that's your real photo on your page then I was probably writing HTML forms while you were still learning to talk. `every form on the internet uses POST method`... no, there's no requirement for that. The "method" attribute of the form determines what method it uses. POST is most common, but occasionally GET is appropriate or at least usable. Anyway that has nothing to do with the fact that the debugging method you're suggesting is primitive, error-prone and ineffecient. – ADyson Dec 31 '20 at 08:59
  • @stillKonfuzed `if request method is GET that WORKS why would a POST not work when changed on both sides similar to GET`...you're assuming that the changes are made correctly. Chances are, if the developer made all the POST/GET changes correctly, then they probably wouldn't have made a mistake in the first place with the original code. And, typos happen. Also, a server could accept POST but not GET for a particular endpoint (or the reverse) - that config can be done at server or application level, where local code changes wouldn't affect it. – ADyson Dec 31 '20 at 09:02
  • @stillKonfuzed trust me, no developer or tester in any of the many professional programming teams I have worked for and with thinks that changing all your code from POST to GET every time you want to test a broken form field is a clever or efficient or even a valid way to work or to do testing. There are far better tools available to test what data is being sent etc, without having to waste time to manually mangle the code, and run the risk or either changing it wrongly, or changing it back wrongly. Again **don't teach the OP these bad habits. They can learn the tools like everyone else.**. – ADyson Dec 31 '20 at 09:04
  • @ADyson impressive profile. But There are times when you need to send a good amount of array data where the value of the key is another array to server where even your `print_r()` and `var_dump()` fails to print the values of the array. I m assuming you must have coded such forms and faced the issue when all you saw was `Array[]` in your console. Please don't be dependent on tools to save 5-10 mins. And there are no good and bad habits or a defined set of rules in debugging you should know that. Keeping track of each and every data that is sent and received is best debugging. – stillKonfuzed Dec 31 '20 at 09:32
  • @ADyson New languages and frameworks are flooding the development ecosystem with different tools and debugging methods every week. Its better to stick to primitive concept so that no matter what framework or language you change to, post and get remains of same and works the same. – stillKonfuzed Dec 31 '20 at 09:38
  • @Soumya I just test your exact code (without the `header(...` line so I could still see the echo messages) in my local server and it works fine. You can see the input, the result, and the data being sent in the developer tools in the pictures here: https://imgur.com/a/ij4ujCC . The code you've shown works as intended. If you have a problem, either you're not submitting a value for the "email" field when you're testing, or the code you're testing is not the code you've shown in your question – ADyson Dec 31 '20 at 09:43
  • @stillKonfuzed `Its better to stick to primitive concept` ...lol. It's a good job all humans don't think like this or we would have nothing to type on here and would be beating each other with clubs and arguing over an animal skin to take home to our caves. Progress is a good thing, Better tools and techniques are a good thing. Sometimes they make things more complex, but usually, if they're good, they save time and effort. See the screenshots I just gave to Soumya - you can see the value of the developer tools. It's easy to look at, it's instant, and it's reliable. Why _wouldn't_ you use it? – ADyson Dec 31 '20 at 09:45
  • @stillKonfuzed var_dump never fails to print the whole object, as far as I'm aware. Can you give an actual example of that? Post it at http://sandbox.onlinephpfunctions.com/ and send me the link when you've got one. `all you saw was Array[] in your console`...just encode the object as JSON and the Javascript console will print it as text. Or you can expand it with the buttons. I'm starting to get the impression that the real issue here is that you haven't spent a few minutes learning to use these tools correctly, and instead you're content to continue to waste time on inefficient processes. – ADyson Dec 31 '20 at 09:46
  • @stillKonfuzed `And there are no good and bad habits` . I'm not sure I agree. Your "change everything to GET" idea is definably bad because it _changes the code_ and therefore you are _not testing the real code_ and therefore you may not see the real issue. And (more of an opinion) it's bad because it's a waste of time. I don't know about your working environment, but in mine time is regarded as precious and we will make attempts to save it wherever we can. And the browser dev tools don't rely on any specific framework or language, since they're client-side only. – ADyson Dec 31 '20 at 09:50
  • @stillKonfuzed In 15 years, i've **never** seen that happen. So again, an example please. Don't make assertions without evidence to back it up...that's not a good look with programmers who rely on knowing exactly what's going on, in order to make their programs work. Maybe you have been using very large objects and suffered from some sort of array-depth issue like the one mentioned [here](https://stackoverflow.com/questions/9998490/how-to-get-xdebug-var-dump-to-show-full-object-array)? – ADyson Dec 31 '20 at 09:51
  • 1
    @ADyson, if you check the html form you will see it has no attributes set so the php script will know its this form, if he changed the input button to email as it is on $_POST, then it supposed to work. – mrSotirow Dec 31 '20 at 09:54
  • @mrSotirow nope. I've just disproved that by running the code - see my comment to soumya a few minutes ago. What specific attributes are you talking about anyway? All the necessary name attributes are present. – ADyson Dec 31 '20 at 09:56
  • @Soumya What you _will_ have a problem with later though is that your usage of mail() is incorrect. Study the [documentation](https://www.php.net/manual/en/function.mail.php) and other examples more carefully. Hint: You are including the message text in the "headers" parameter. Message and headers are separately. (Actually my recommendation would be to use PhpMailer instead of mail(), it's easier to construct and send messages correctly without having to know intricate details of the header formats etc.) – ADyson Dec 31 '20 at 10:02

1 Answers1

0

nothing wrong with your code, i guess you doing a direct request to mail.php

try this code, maybe this help your problem

<?php
if($_POST){ // add condition if http request is POST then process your POST request
    if (isset($_POST['email'])){
    
    echo "submit is set to {$_POST['submit']} and now we send the email<br>";

$to = "abc@email.com"; 
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";


if(mail ($to, $subject, $name,  $message)){
    echo "Enquiry sent successfully!<br>";
   
}
    else
    {
    echo "Mail was not sent. Please try again later<br>";
    }
} else{

echo"submit is not set<br>"; 
}

echo "after the if condition";


exit; // if POST process has done, script will stop, and code above will not showing
}

?> 
<form id="contact-form" method="POST" action="">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Name</label>
                                <input class="form-control" name="name" id="name" placeholder="" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Email</label>
                                <input class="form-control" name="email" id="email" placeholder="" type="email" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Subject</label>
                                <input class="form-control" name="subject" id="subject" placeholder="" required>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Message</label>
                        <textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
                    </div>
                    <div class="text-right"><br>
                    
                        
                        <input class="btn btn-primary solid blank button"  id="btn" type="submit" value="submit" name="submit">
                    
                    </div>
    
                </form>
Anas Fanani
  • 685
  • 7
  • 19
  • 1
    Specifically what did you change and why? Don't just provide a big lump of code... explain it so the OP and everyone else can actually learn from it. – ADyson Dec 30 '20 at 23:56
  • This is better, thanks. But I don't think this is the issue. OP says they are seeing the "submit is not set" text _after_ they submit the form. So i don't think the problem is with showing the message during an initial GET request. It looks like they are submitting from a different script (contact.php) anyway, so there'd never be a GET to mail.php in expected usage. I would guess either the OP is not testing it properly, or they are testing code which is different from what's shown in the question. Also, their usage of mail() is incorrect but that's a separate issue. – ADyson Dec 31 '20 at 10:00