0

I am using a form in php file inside a modal window like this

 <div class="modal-body">
                <form  method="post" action="m.php">
                    <div class="form-group">
                        <lable>Name</lable>
                        <input type="text" class="form-control" required="required">
                    </div>

                    <div class="form-group">
                        <lable>Email</lable>
                        <input type="text" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <lable>Details</lable>
                        <input type="text" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <lable>Message</lable>
                        <textarea name="" id=""  class="form-control"></textarea>
                    </div>
                     <div class="form-group">
                        <a href="" class="btn btn-default" name="submit" style=" background: #eee; width:100px;display: block;margin-left:auto;">Submit</a>
                    </div>
                </form>
            </div>

file name is m.php now when i press the submit button it doesn't detect form submission

<?php

if( ($_SERVER['REQUEST_METHOD'] == 'POST')){
    die("form submitted");
}

?>

i tried $_POST['submit'] as well kindly help me whats wrong with it

3 Answers 3

5

This is because you dont press a submit button, you press a link called submit. Try replacing it with <input type="submit" value="Submit" />.

Also, your check wether your form has been submitted uses a dangerous method. If you have multiple forms to be handled in your code, it will catch them all. A better approach would be:

if( isset($_POST['nameOfSubmitbutton']) ){}
// because you can now easily do:
if( isset($_POST['completelyDifferentButton']) ){}

If you want to keep the anchor (I advice against it), you can use javascript to fake the submit for you:

document.getElementById('yourAnchor').onclick = function(){
    document.getElementById('yourForm').submit();
}
// Or if you have jQuery:
$('#yourAnchor').on('click', function(e){
    e.preventDefault();
    $(this).closest('form').submit();
});
Sign up to request clarification or add additional context in comments.

2 Comments

or using JQuery $(".submit").on("click",function(e){ e.preventDefault(); $("form").submit() };
I've added a jquery version as well, slightly better (note the closest())
1

The submit button you are using is pointing towards the same url, can you try something like?

<input class="btn btn-default" type="submit" value="Submit">

and then try to see if the form is submitted from PHP. And i also would recommend that you use some framework for this to handle sql injections and/or other exploits.

2 Comments

i use ci this was a project i was supposed to do changes , actually i forgot to do it in custom because of doing with ci and laravel since so long :D
1

Wrong use of form submit.

 <div class="form-group">
                    <a href="" class="btn btn-default" name="submit" style=" background: #eee; width:100px;display: block;margin-left:auto;">Submit</a>
 </div>

You should use it like:-

<div class="form-group">
      <input type="submit" class="btn btn-default" name="submit" style="background: #eee; width:100px;display: block;margin-left:auto;" value="Submit"/>
</div>

Hopefully this will solve the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.