2010-01-02 58 views
3

我有poll.php标准形式:PHP形式的Ajax类型

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

反正是有处理表单,而不会导致用户createpoll.php,像调用createpoll.php上提交?

回答

2

该技术被称为AJAX。在JAvaScript库的帮助下,使用它变得非常简单。您可以使用JQueryPrototype。搜索AJAX提交。这个主题有很多答案 - 即stackoverflow questions

对于exapmle,使用JQuery方法ajax()它看起来像这样(的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. 
    } 
}); 
0

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

礼貌您也可以在同一个站点的obj2query功能。

<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; 
} 
1

在你的形式使用Ajax一个伟大的,非常简单的方法可以在这里找到:http://jquery.malsup.com/form/

+0

我实际使用这个插件在一些项目上工作得很好。 – 2010-01-02 01:27:47