..blah...Is there a way ...">

PHP form for Ajax type

I have a standard form on poll.php:

    <form method="POST" action="createpoll.php">
      ..blah...
    </form>

Is there a way to process the form without causing the user to createpoll.php, something like calling createpoll.php on submit?

+3
source share
4 answers

This technology is called AJAX . Using JAvaScript libraries, it becomes very easy to use it. You can use jQuery or Prototype , find the AJAX feed. There are many answers on this topic, that is, questions about the glass surface .

For exapmle, using the jQuery ajax () method , it looks like this (JavaScript):

$.ajax({  
        type: "GET",                        // method - Get or Post
    url: "cart.php",                    //  Url to send data
    data: { addproduct: productIDVal, isAjax: 'true'},  // Parameters
    success: function(theResponse) {           
        // code to operate with response, if the request was succesful. 
            // It can be string or array.
    }  
}); 
+2
source
+1

, : http://onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html

You will need to capture the form submission using JavaScript, submit the data using XMLHttpRequest (XHR) and parse the response.

0
source

Courtesy of http://js.isite.net.au/snippets/form2obj

You can also find the feature obj2queryon the same site.

<form action="submit.here" method="POST" onsubmit="submit_via_xhr( this.method,
this.action, obj2query( form2obj( this ) ), successFunction ); return false">

function form2obj(theForm) {
   var rv = {};

   if (typeof(theForm) == 'string')
      theForm = document.getElementById(theForm);

   if (theForm) {
      for (var i = 0; i < theForm.elements.length; i++) {
         var el = theForm.elements[i];
         if (el.name) {
            var pushValue = undefined;
            if (
               (el.tagName.toUpperCase() == 'INPUT'
                  && el.type.match(/^text|hidden|password$/i))
               || el.tagName.toUpperCase() == 'TEXTAREA'
               || (el.type.match(/^CHECKBOX|RADIO$/i) && el.checked)
            ){
               pushValue = el.value.length > 0 ? el.value : undefined;
            }
            else if (el.tagName.toUpperCase() == 'SELECT') {
               if( el.multiple ) {
                  var pushValue = [];
                  for( var j = 0; j < el.options.length; j++ )
                     if( el.options[j].selected )
                        pushValue.push( el.options[j].value );
                  if( pushValue.length == 0 ) pushValue = undefined;
               } else {
                  pushValue = el.options[el.selectedIndex].value;
               }
            }
            if( pushValue != undefined ){
               if(rv.hasOwnProperty( el.name ))
                  if( rv[el.name] instanceof Array ) {
                     rv[el.name] = rv[el.name].concat( pushValue );
                  }
                  else {
                     rv[el.name] = [].concat( rv[el.name], pushValue );
                  }
               else {
                  rv[el.name] = el.value;
               }
            }
         }
      }
   }
   return rv;
}
0
source

Source: https://habr.com/ru/post/1727071/


All Articles