douqianzha6213 2017-07-20 20:28
浏览 65

为每个mysql使用javascript创建提交

Currently the table shows a different button for each mysql row, but only the first mysql row button works properly. When it works properly, the submit button saves the location row as a value for a javascript AJAX post. When it doesn't work properly the submit button refreshes the page.

This is the html and mysql form:

   while($row = mysqli_fetch_array($result)) {

echo '<form class="form-signin" action="" method="POST" id="register-form"><tr>';
echo "<td><b>" . $row['product'] . "</b><br>";
echo "<td><b>" . $row['location'] . "</b><br>";
echo "" . $row['notes'] . "";
  echo '  
  <input type="hidden" name="user_name" value="'.   $row['product'].'" />
   <input type="hidden" name="location" value="'.   $row['location'].'" />
  <input type="text" class="form-control" placeholder="Table Number" name="user_email" id="user_email" />
  <input type="text" class="form-control" placeholder="Quantity" name="password" id="user_email" />
   <button type="submit" class="btn btn-default" value="Submit form" name="btn-save" id="btn-submit">
                <span class="glyphicon glyphicon-log-in"></span> &nbsp;'. $row['location'] .'
            </button></td>';

echo "</tr></form>";

} 

This is the javascript for "register-form" that posts:

{
    var data = $("#register-form").serialize();

    $.ajax({

        type: 'POST',
        url: 'register.php',
        data: data,
        beforeSend: function() {
            $("#error").fadeOut();
            $("#btn-submit").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
        },
        success: function(data) {}
    })
}

Basically if the submit button is clicked need it to save for the respective row that is clicked, instead of taking a different action.

$('document').ready(function()
{
    /* validation */
    $("#register-form").validate({
        rules:
        {

        },
        messages:
        {
            user_name: "Enter a Valid Username",
            password:{
                required: "Provide a Password",
                minlength: "Password Needs To Be Minimum of 8 Characters"
            },
            user_email: "Enter a Valid Email",
            cpassword:{
                required: "Retype Your Password",
                equalTo: "Password Mismatch! Retype"
            }
        },
        submitHandler: submitForm
    });
    /* validation */

    /* form submit */
    function submitForm()
    {
        var data = $("#register-form").serialize();

        $.ajax({

            type : 'POST',
            url  : 'register.php',
            data : data,
            beforeSend: function()
            {
                $("#error").fadeOut();
                $("#btn-submit").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
            },
            success :  function(data)
            {
                if(data==1){

                    $("#error").fadeIn(1000, function(){


                        $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Sorry email already taken !</div>');

                        $("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                    });

                }
                else if(data=="registered")
                {

                    $("#btn-submit").html('Ordered');
                   // setTimeout('$(".form-signin").fadeOut(500, function(){ $(".signin-form").load(""); }); ',5000);

                }
                else{

                    $("#error").fadeIn(1000, function(){

                        $("#error").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+data+' !</div>');

                        $("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                    });

                }
            }
        });
        return false;
    }
    /* form submit */

});
  • 写回答

1条回答 默认 最新

  • dongzhang8475 2017-07-20 20:32
    关注

    I am pretty sure it is simply because you have all of the buttons with the same id which I think means all but the first will not be valid. Try adding the row id to the end of the id="btn-submit" and see if that works.

    EDIT::

    The javascript doesn't do anything because you are probably starting your click handler with something like:

     $('#submit-btn')... 
    

    right? (I cant see that part of the javascript in what you copied above.) If that is the case, what that little piece of codes says is

    find an element with the id=submit-btn and then run this code when that element is clicked.

    What you need to have is: $('.row-submit-btn').... instead. This is saying find an element with the css class of row-submit-btn and then run this code when one of them is clicked.

    The reason I say to use a CSS class instead of the id is because id's have to be unique or the html is invalid (thus the reason this question is here!) but CSS Classes do not have this restriction. The major caveat here is that you need to make sure that this css class is unique to elements on this page (meaning there aren't any other things with row-submit-btn as a css class.) otherwise you will get unintended side effects.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line