首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用AJAX追加PHP代码

用AJAX追加PHP代码
EN

Stack Overflow用户
提问于 2013-09-25 19:27:33
回答 2查看 4.4K关注 0票数 1

当用户单击按钮(+)时,我希望在div中插入一个新字段。

textfield的代码是:

代码语言:javascript
运行
复制
<?php
$sql = "SELECT nome, codigo FROM ref_bibliograficas";
$result = mysql_query($sql) or die (mysql_error());
echo ("<select class='autocomplete big' name='ref_bib_0' style='width:690px;' required>");
echo ("<option select='selected' value=''/>");  
while($row = mysql_fetch_assoc($result)){
echo ("<option value=" . $row["codigo"] . ">" . $row["nome"] . "</option>");
echo ("</select>");
mysql_free_result($result);
?>

因此,我不知道如何使用AJAX插入字段。

我用jQuery把这个函数按一下!有人能帮我吗?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2013-09-25 19:33:42

您要寻找的是jQuery .load()函数。http://api.jquery.com/load/

让您的php页面输出您想要添加到div中的所需的HTML,那么您的JavaScript代码应该如下所示:

代码语言:javascript
运行
复制
$('#addButton').click(function(){          // Click event handler for the + button. Replace #addButton wit the actual id of your + button
    $('#myDiv').load('yourphppage.php');   // This loads the output of your php page into your div. Replace #myDiv with the actual id of your div
});

如果要在div中添加一个新字段,则应执行以下操作:

代码语言:javascript
运行
复制
$('#addButton').click(function(){  
    $.post('yourphppage.php', function(data) {
        $('#myDiv').append(data);
    });
});
票数 1
EN

Stack Overflow用户

发布于 2013-09-25 19:37:40

Ajax方法

代码语言:javascript
运行
复制
$(document).ready(function(e)
{
  $('#plus-button').click(function(e)
  {
    $.ajax(
    {
    url: "PHP-PAGE-PATH.php", // path to your PHP file
    dataType:"html",
    success: function(data)
    {
       // If you want to add the data at the bottom of the <div> use .append()

       $('#load-into-div').append(data); // load-into-div is the ID of the DIV where you load the <select>

       // Or if you want to add the data at the top of the div

       $('#load-into-div').prepend(data); // Prepend will add the new data at the top of the selector
    } // success
    }); // ajax
   }

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19013547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档