Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest Way To Make A Form Submit Without Refresh

I have been trying to create a simple calculator. Using PHP I managed to get the values from input fields and jump menus from the POST, but of course the form refreshes upon submit.

Using Javascript i tried using

function changeText(){
document.getElementById('result').innerHTML = '<?php echo "$result";?>'

but this would keep giving an answer of "0" after clicking the button because it could not get values from POST as the form had not been submitted.

So I am trying to work out either the Easiest Way to do it via ajax or something similar

or to get the selected values on the jump menu's with JavaScript.

I have read some of the ajax examples online but they are quite confusing (not familiar with the language)

like image 897
James Avatar asked Sep 08 '10 10:09

James


People also ask

Can you submit a form without reloading the page?

If you have ever wanted to send a form without reloading the page, provide a look-ahead search function that prompts the user with suggestions as they type, or auto-save documents, then what you need is AJAX (also known as XHR). A behind-the-scenes request is sent to the server, and returning data to your form.

How do I make a form submit not reload?

Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload.

How do I automatically submit a form without clicking?

Submit a Form Using JavaScript The most simple way to submit a form without the submit button is to trigger the submit event of a form using JavaScript. In the below example we are going to create a function to submit a form. We will set that function at onclick event of a div tag.

How do I make my HTML page not refresh?

there is no need to js or jquery. to stop page reloading just specify the button type as 'button'. if you dont specify the button type, browser will set it to 'reset' or 'submit' witch cause to page reload. This does work! It's a good alternative to <input type='button' /> .


2 Answers

Use jQuery + JSON combination to submit a form something like this:

test.php:

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="jsFile.js"></script>

<form action='_test.php' method='post' class='ajaxform'>
 <input type='text' name='txt' value='Test Text'>
 <input type='submit' value='submit'>
</form>

<div id='testDiv'>Result comes here..</div>

_test.php:

<?php
      $arr = array( 'testDiv' => $_POST['txt'] );
      echo json_encode( $arr );
?>

jsFile.js

jQuery(document).ready(function(){

    jQuery('.ajaxform').submit( function() {

        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        for(var id in data) {
                            jQuery('#' + id).html( data[id] );
                        }
                      }
        });

        return false;
    });

});
like image 89
Naveed Avatar answered Sep 19 '22 18:09

Naveed


The best way to do this is with Ajax and jQuery

after you have include your jQuery library in your head, use something like the following

$('#someForm').submit(function(){
   var form = $(this);

   var serialized = form.serialize();

   $.post('ajax/register.php',{payload:serialized},function(response){
       //response is the result from the server.
       if(response)
       {
           //Place the response after the form and remove the form.
           form.after(response).remove();
       }
   });
   //Return false to prevent the page from changing.
   return false;
});

Your php would be like so.

<?php
    if($_POST)
    {
       /*
           Process data...
       */


       if($registration_ok)
       {
           echo '<div class="success">Thankyou</a>';
           die();
       }
    }
?>
like image 22
RobertPitt Avatar answered Sep 18 '22 18:09

RobertPitt