2010-09-16 70 views
1

将以下PHP表单提交转换为Ajaxy类型表单有多简单。PHP表单提交 - 转换为jQuery .Ajax

这里是PHP:

<?php //Send Tweet 
    if(isset($_POST['submit'])) { 
    $qtweet = $_REQUEST['tweet']; 
    $connection->post('statuses/update', array('status' => $qtweet)); 
    echo "<div style='padding-bottom: 5px; color: #0099FF;'>Updated your Timeline Successfully.</div>"; 
    } 
?> 

和HTML表单:

<form id="tweethis" method='post' action='index.php'> 
    <textarea style="width: 346px;" name="tweet" rows="5" id="tweet" ></textarea> 
    <br /> 
    <span id="charLeft">140</span> Characters left 
    <br /> 
    <input type='submit' value='Tweet This!' name='submit' id='submit' /> 
    <br /> 
</form> 

我想这样的页面犯规需要重新加载使用Ajax。

回答

0

很容易。

使用$ .ajax,并检出serialize()函数以转换您的表单数据。您还需要防止默认行为和事件冒泡。这tutorial是相当不错的。

在ajax调用中,您必须指定处理请求的PHP页面。该脚本将处理表单提交,就像正常的PHP表单一样,然后回应任何输出。该输出将被传回给呼叫客户端。

1
$(document).ready(function() { 
    $("#tweethis").submit(function() { 
     $.post('index.php', $("#tweet").val(), function(html) { 
      $("#resultDiv").html(html); 
     }); 
     return false; // prevent normal submit 
    }); 
}); 

看看$.ajax$.post(和$.get)包装它。