1

I have a simple PHP form like

<form action="" id="searchForm">
            <input type="text" id="name" name="theName" placeholder="Your Name" />
            <button id="send">Send data</button>
 </form>

and I am using ajax to send the POST values to the PHP file

    $("#send").on("click", function(){
     var name= $('#theName').val();
     var data='name='+name; 
     var request = $.ajax({
                           type: "POST",
                           url: "assets/map.php",
                           data: data,
                           cache: false
      });

    });

This works correctly when using JavaScript but I am thinking what if the JS was disabled in a browser? How we can still send the data to the server? should I add the map.php file into the form action attribute? if so how to prevent sending double POST[] values?

3
  • 1
    You should add the map.php in your action form to make it work for browser without JS, don't forget to change your map.php to do a redirect if the request type if GET Commented Aug 15, 2015 at 9:40
  • 1
    than there is no use of ajax ask user to enable js or just move on Commented Aug 15, 2015 at 9:40
  • 2
    Put the URL in your forms action and just prevent the default action in your JS function. Commented Aug 15, 2015 at 9:42

2 Answers 2

3

You should use onsubmit and prevent the default action (the ordinary submit action) for the JS part, then indeed fill in the action attribute with the same url that you use for the ajax post.

$("#searchForm").submit(function(e){
     e.preventDefault();

     var name= $('#theName').val();
     var data='name='+name; 
     var request = $.ajax({
                       type: "POST",
                       url: "assets/map.php",
                       data: data,
                       cache: false
      });

    });

And the HTML:

<form action="assets/map.php" id="searchForm" method="post">
        <input type="text" id="name" name="theName" placeholder="Your Name" />
        <button id="send" type="submit">Send data</button>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

You should probably also mention to put method="post" on the form. Otherwise the data will be send as $_GET variables when JS is disabled.
1

First add assets/map.php to your action.

Then change your jquery to this:

$("#send").on("click", function(){
    var name= $('#theName').val();
    var data='name='+name; 
    var request = $.ajax({
                         type: "POST",
                         url: "assets/map.php",
                         data: data,
                         cache: false
    });
    return false; //prevents action
});

As you can see this will only prevent the action if the jquery has ran.

1 Comment

assets/map.php (as in the url: "assets/map.php")

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.