如何在单击提交后使用ajax更新特定的div?

时间:2017-09-03 20:54:37

标签: php ajax

<?php    
$short_des="Harry Potter is a series of fantasy novels"; //initially short_description has this value
echo '<span><p>About:</p> '.$short_des.'</span>'; //initially i am printing short_des here but after cick of submit i want to update it
echo '<form class="make_long_form" method="post">';
    echo '<input type="text" id="make_descrip_long" method="post" name="make_descrip_long" value="'.$post_read_more['id'].'">';
    echo '<input type="submit" id="read_more_button" value="Show more">'; //this is the submit form
echo '</form>';
if(isset($_POST['make_descrip_long'])){
$short_description = "Harry Potter is a series of fantasy novels written by British author J. K. Rowling."; //after the click of submit i want to update the value of short_description and update it in the about echo above
                        }   
?>

我想我必须使用ajax来更新echo中short_des的值。帮助

1 个答案:

答案 0 :(得分:0)

如果您使用的是jQuery,请尝试以下方法:

ajax功能:

function loadDiv(url, element){
    $.post(url, {}, function(res){
        myDiv.html(res);
    }
}

url:是一个字符串,它显示您要放入div中的数据所在的页面。

元素:是你要更新的div,在jQuery中你可以用它的id选择器传递它:

var element = $("#divID");

{}是一个json对象,将作为post传递给url页面。

在res变量中,您可以在html中获得请求的响应。