不插入数据库

时间:2014-04-11 04:34:20

标签: php html ajax

请记住,这是一个AJAX表单数据提交。

HTML:

<input type="radio" id="avatar1" name="avatar" class="userpass" value="Option1"/>
<input type="radio" id="avatar2" name="avatar" class="userpass" value="Option2"/>

AJAX:

if(isset($_POST['avatar'])){
    if($_POST['avatar'] == "Option1"){
       $selected_avatar = "images/option1.png";
    }
    else{
        $selected_avatar = "images/option2.png";
    }
}
    $avatar = $_GET['avatar'];
    $form_name = $_GET['form_name'];
    $form_comment = htmlspecialchars($_GET['form_comment']);
    $date = date('F j, Y g:ia');

    $ip = $_SERVER['REMOTE_ADDR'];

     if($form_name == '') {
        echo("<div class='alert alert-error-x'>Don't forget to enter your name, as we need to identify who's commenting on this article!</div>");

    } else if($form_comment == '') {
        echo("<div class='alert alert-error-x'>Please do not leave the comment field blank, we want to know what you're saying!</div>");


    } else {
mysql_query("INSERT INTO comment (id, articleid, date, name, comment, avatar, ip) VALUES (NULL,'{$_GET['id']}','{$date}','{$form_name}','{$form_comment}','{$avatar}','{$ip}')");

除了单选按钮选项外,它还会输入数据库中的所有其他内容。

使用Javascript:

<script type="text/javascript">
$(function() {

 $('#reset_form').click(function() {
     $('#name,#comment,#avatar').val('');
 });
 $('#submit').click(function() {

var name = $('#name').val();
var comment = $('#comment').val();
var avatar = $('#avatar').val();

$.ajax({
    url: '../forms/comment_ajax.php?id=<?php echo $_GET['id']; ?>',
    data: { form_name: name, form_comment: comment, form_avatar: avatar },
    success: function(data) {

        $(document).trigger('close.facebox');
        $('#new_comment').append(data);

        $('#new_comment').effect("bounce", { direction:'down', times:5 }, 300);        
        $('html,body').animate({scrollTop:0}, 2000, "easeOutQuart");
        return false;
    }
});
});                   
});
</script>

0 个答案:

没有答案