PHP AJAX表单提交显示数组

时间:2022-11-24 14:55:31

When I submit my form to post a comment on my page the page displays the array of data and does not just display the page with the comment fading in.

当我提交表单以在我的页面上发表评论时,页面显示数据数组,而不仅仅显示评论淡入的页面。

This is what i see in my browser after i hit the submit button (the record gets inserted fine):

这是我点击提交按钮后我在浏览器中看到的内容(记录插入正常):

{"streamid":1567,"cm":{"id":"0","avatar":"uploads\/30-av.jpg","name":"Mike<\/a>","content":"blah blah blah","time":"some seconds ago"}}

PHP

$action = $_GET['action'];
if ($action == 'blasting') {
$blast = input($_POST['blast']);
if ($blast AND strlen($blast) > 1) {
    $stm = $_DB->prepare('insert into wl_activity (user_id, action, posted_on, data) values (?, ?, ?, ?)');
    $stm->execute(array($_USER->get_user_id(), 'blast', time(), $blast));
    }
echo json_encode($array);
   }

My Form code

我的表格代码

    <script type="text/javascript" src="js/ajaxupload.js"></script>
    <script type="text/javascript" src="js/jquery.form.js"></script>
     <script type="text/javascript" src="js/newsfeed.js"></script>

 <form method="post" action="index.php?cmd=ajax&action=blasting" id="blast-form">
            <textarea class="form_input" id="blastbox" name="blast" style="width:90%" ></textarea>
            <input type="submit" id="blastbtn" value="Post" class="form_submit" />

<div id="ajax-feed"></div>

    <?php $general = array(); ?>
    <?php
        foreach ($feeds as $f) {
            if ($f['action'] == 'blast')
                array_push($general, 'blast');
                //unset($general[count($general)-1]);
            else
                array_push($general, 'normal');
        }
    ?>

Here is my jquery.form.js http://pastebin.com/MP3uVdcP Here is my newsfeed.js that http://pastebin.com/ymSNjXJR

这是我的jquery.form.js http://pastebin.com/MP3uVdcP这是我的newsfeed.js http://pastebin.com/ymSNjXJR

1 个解决方案

#1


0  

Your form is just a simple form with no Ajax functionality attached (as far as we can see in your example).

您的表单只是一个没有附加Ajax功能的简单表单(在您的示例中我们可以看到)。

Your browser is successfully posting the form by moving to index.php?cmd=ajax&action=blasting, which is where you see your JSON response.

您的浏览器通过移动到index.php成功发布表单?cmd = ajax&action = blasting,这是您看到JSON响应的地方。

You need to use JavaScript to hook into the form submission event, stop the default action (moving to the next page) and send it using Ajax.

您需要使用JavaScript挂钩到表单提交事件,停止默认操作(转到下一页)并使用Ajax发送它。

Here is an example:

这是一个例子:

$("#blast-form").submit(function(e){
    e.preventDefault();
    $.post($(this).action, $(this).serialize(), function(response) {
        $('#ajax-feed').html(response);
    });
});

#1


0  

Your form is just a simple form with no Ajax functionality attached (as far as we can see in your example).

您的表单只是一个没有附加Ajax功能的简单表单(在您的示例中我们可以看到)。

Your browser is successfully posting the form by moving to index.php?cmd=ajax&action=blasting, which is where you see your JSON response.

您的浏览器通过移动到index.php成功发布表单?cmd = ajax&action = blasting,这是您看到JSON响应的地方。

You need to use JavaScript to hook into the form submission event, stop the default action (moving to the next page) and send it using Ajax.

您需要使用JavaScript挂钩到表单提交事件,停止默认操作(转到下一页)并使用Ajax发送它。

Here is an example:

这是一个例子:

$("#blast-form").submit(function(e){
    e.preventDefault();
    $.post($(this).action, $(this).serialize(), function(response) {
        $('#ajax-feed').html(response);
    });
});