无法使用ajax发布多个变量

时间:2013-08-29 11:56:38

标签: php ajax jquery mysqli

我只能传递此代码中的一个变量。如果看起来像这样,我只能传递firstname变量。当我意外拼写错误fistname时(当我设置要发布的数据时),只传递了lastname变量。所以我希望能够发布两个属于我的问题的变量

var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
$.ajax({
    type: "POST",
    url: "modify_profile.php",
    data: { firstname: firstname, lastname: lastname },
    success: function( data){             
    }
});

我也试过写:data:{“firstname”:firstname,“lastname”:lastname},但这也不起作用。

这是完整的代码。这是我的文件modify_profile.php。一切都发生在这个文件中。

 <!Doctype HTML>
    <html>
        <head>
            <title> Resor </title>
        </head>
        <body>
        <?php
        require("../db/connect.php");
        $email='test@gmail.com';


        // Hämtar all data från personen som är inloggad
        $sql="SELECT * from user WHERE email='$email'";
        $query=mysqli_query($dbcon,$sql);
        $result=mysqli_fetch_array($query);

        $firstname=$result['first_name'];
        $lastname=$result['last_name'];
        $password=$result['password'];



        // HERE IS WHERE FIRSTNAME IS CHANGED IN CASE IT HAS BEEN UPDATED.
            if( isset($_POST['firstname'])) 
            {

        $firstname=( $_POST['firstname']);
        $query=mysqli_query($dbcon, "UPDATE user SET first_name='$firstname' WHERE     email='$email'");
         $result = mysqli_query($dbcon,$query);
          if (!$result) {
              die("MySQL error ".mysqli_errno().": ".mysqli_error()."\n<br>When executing  <br>\n$query\n<br>");
          }

          }
//  HERE IS WHERE LASTNAME IS CHANGED IN CASE IT HAS BEEN UPDATED.
        if( isset($_POST['lastname']))
           {

        $lastname=( $_POST['lastname']);
        $query=mysqli_query($dbcon, "UPDATE user SET last_name='$lastname' WHERE email='$email'");
        $result = mysqli_query($dbcon,$query);
          if (!$result) {
              die("MySQL error ".mysqli_errno().": ".mysqli_error()."\n<br>When executing <br>\n$query\n<br>");
          }

          }



            echo "<p> Namn: $firstname $lastname  <span id=name> &Auml;ndra</span> </p>";






             ?>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">   </script>
            <script>
            $(document).ready(function(){



                $('#name').on('click', function() {
                    var node = "<form action='modify_profile.php' method='post'><p>F&ouml;rnamn: <input type='text' name='firstname' id='firstname'value='<?=$firstname?>' ></p><p>Efternamn: <input type='text' name='lastname' id='lastname' value='<?=$lastname?>'></p><p>V&auml;nligen mata in ditt l&ouml;senord f&ouml;r att bekr&auml;fta &auml;ndringen.</p> <p> L&ouml;senord:<input type='password' name='password'  id='password'></p><input type='button' value='Spara &auml;ndringar' id='confirm_name'/><input type='button' value='Avbryt' id='cancel_name_change' /></form>";





                    $(this).parent().after(node);
                    $(this).parent().remove();

                }); 

                            //if the confirm button has been pressed
                $(document).on('click', '#confirm_name', function(){
                var firstname = $("#firstname").val();
                var lastname=$("#lastname").val();
                var password=$("#password").val();


                if(password!='<? echo "$password"; ?>'){
                alert("No changes has been made due to wrong password.");
                }
                else{
                    $.ajax({
                        type: "POST",
                        url: "modify_profile.php",
                      data: {"firstname":firstname,"lastname":lastname},
                        success: function( data){     
                        }
                    }); 
                }
                });

                // if interrupted 

                $(document).on('click', '#cancel_name_change', function(){

                location.reload();

                });

                //slutparentes
            });
            </script>
            <?php require("../db/close.php")
            ?>
        </body> 
    </html>

0 个答案:

没有答案