简体   繁体   中英

Is there any better way to work with ajax,php and mysql than this?

Let's suppose that I want to have the following small code

<div id="field"> Some links here with a database call. </div>

The problem is that I want to be able to work with ajax on them and call them when the user adds a new link through the appropriate form.

So instead of having this div, I use a function and call it whenever needed using ajax.

     <?php  
  function testfield ($id) {  Database call
     ?>
      <div id="field"> <?php The links in here  ?> </div>
      }
   <html>
      <body>
        <?php testfield($_GET['id']); ?> 
     </body>
   </html>

So when a user adds a new link, I just call with ajax the function and I'm set.

Is there any other better way to do this? Thank you in advance

For example you should use JSON and return data into your div. For AJAX try use jQuery.

PHP code:

$linkarray = array();
$linkarray[] = 'link1';
$linkarray[] = 'link2';
$linkarray[] = 'link3';
echo json_encode($linkarray);

HTML:

<html>
<body><div id="linklist"></div></body>
</html>

JS:

$.ajax({
 url:'getlinklist.php',
        dataType:'json',
        type:'POST',
        success: function(data){
            $('#linklist').html('');
            $.each(datamfunction(key,value){
                $('#linklist').append(value+'<br/>');                 
            });
        }
    });

当用户添加新链接时,您将使用AJAX将数据发送到与数据库一起使用的PHP脚本,如果成功,则应使用javascript刷新DIV-在DIV末尾写入新信息(您已经拥有此信息),或使用信息,您从JSON大量获取,从PHP脚本成功返回

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM