Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么javascript在php中将这些变量作为对象提供?_Javascript_Php_Html_Mysql - Fatal编程技术网

为什么javascript在php中将这些变量作为对象提供?

为什么javascript在php中将这些变量作为对象提供?,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我试图将一些变量传递给PHP文件,以便运行SQL查询。但是JavaScript将这些变量作为对象传递。为什么会这样?我怎样才能改变这一点 <?php include 'dbconfig.php';?> <?php header('Content-Type: text/html; charset=utf-8');?> <!DOCTYPE HTML PUCLIC "-//W3C//DTDHTML 4.0 Transitional//EN"> <HTML

我试图将一些变量传递给PHP文件,以便运行SQL查询。但是JavaScript将这些变量作为对象传递。为什么会这样?我怎样才能改变这一点

  <?php include 'dbconfig.php';?>
<?php header('Content-Type: text/html; charset=utf-8');?>

<!DOCTYPE HTML PUCLIC "-//W3C//DTDHTML 4.0 Transitional//EN">
<HTML>
<HEAD>
    <TITLE>LIBRARY DATABASE</TITLE>
    <link rel="stylesheet" type="text/css" href="mytable.css">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <link rel="stylesheet" type="text/css" href="jquerycss.css">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(function () { 

            var new_dialog = function (type, row) {
            var dlg = $("#dialog-form").clone();
            var memberID = dlg.find(("#memberID")),
            MFirst = dlg.find(("#MFirst")),
            MLast = dlg.find(("#Mlast")),
            Street = dlg.find(("#Street")),
            number = dlg.find(("#number")),
            postalCode = dlg.find(("#postalCode")),
            Mbirthdate = dlg.find(("#Mbirthdate"));
                type = type || 'Create';
                var config = {
                    autoOpen: true,
                    height: 500,
                    width: 600,
                    modal: true,
                    buttons: {
                        "Create an account": save_data,
                        "Cancel": function () {
                            dlg.dialog("close");
                        }
                    },
                    close: function () {
                        dlg.remove();
                    }
                };
                if (type === 'Edit') {
                    config.title = "Edit User";
                    get_data();
                    delete (config.buttons['Create an account']);
                    config.buttons['Edit account'] = function () {
                        window.open("edituple.php?memberID="+ memberID +"&MFirst=" + MFirst +"&MLast=" + MLast +"&Street=" + Street
                        +"&number=" + number + "&postalCode=" + postalCode +"&Mbirthdate=" + Mbirthdate);
                        dlg.dialog("close");
                    }; 
                }
                dlg.dialog(config); 
                function get_data() {
                    var _memberID = $(row.children().get(1)).text(),
                     _MFirst = $(row.children().get(2)).text(),
                     _MLast = $(row.children().get(3)).text(),
                    _Street = $(row.children().get(4)).text(),
                    _number = $(row.children().get(5)).text(),
                    _postalCode = $(row.children().get(6)).text(),
                    _Mbirthdate = $(row.children().get(7)).text();
                    memberID.val(_memberID);
                    MFirst.val(_MFirst);
                    MLast.val(_MLast); 
                    Street.val(_Street);
                    number.val(_number); 
                    postalCode.val(_postalCode);
                    Mbirthdate.val(_Mbirthdate); 
                } 
                function save_data() {
                    $("#users tbody").append("<tr>" + "<td>" + (fname.find("option:selected").text() + ' ').concat(lname.find("option:selected").text()) + "</td>" + "<td>" + email.val() + "</td>" + "<td>" + password.val() + "</td>" + "<td><a href='' class='edit'>Edit</a></td>" + "<td><span class='delete'><a href=''>Delete</a></span></td>" + "</tr>");
                    dlg.dialog("close");
                }
            }; 
            $(document).on('click', 'span.delete', function () {
                $(this).closest('tr').find('td').fadeOut(1000, 
        function () {
            // alert($(this).text());
            $(this).parents('tr:first').remove();
        }); 
                return false;
            });
            $(document).on('click', 'td a.edit', function () {
                new_dialog('Edit', $(this).parents('tr'));
                return false;
            }); 
            $("#create-user").button().click(new_dialog); 
        });
    </script>
</HEAD>
<BODY>
<div class="navbar">
                <a href="mydatabase.php">Home</a>
                <a href="member_table.php">Member</a>
                <a href="book_table.php">Book</a>
                <a href="borrows_table.php">Borrows</a>
              </div>
              <br><br>
              <div class="navbar2">
                    <a href="insert.php">Insert</a>
                    <a href="update.php">Update</a>
                    <a href="delete.php">Delete</a>
                  </div>

    <TABLE class="minimalistBlack">
        <thead>
        <tr>
        <th> memberID </th>
        <th> First Name </th>
        <th> Last Name </th>
        <th> Street </th>
        <th> Number </th>
        <th> Postal Code </th>
        <th> Birthdate </th>
        <th> Update </th>
        </tr>
        </thead>
        <?php 
            $conn= mysqli_connect("localhost","root","","library");
            mysqli_set_charset($conn, "utf8");

            if ($conn -> connect_error){
                die("Conenction failed:". $conn->connect_error);
            }
            $sql="SELECT memberID,MFirst,MLast,Street,number,postalCode,Mbirthdate FROM member";
            $result = $conn->query($sql);
            if ($result->num_rows>0){

                while($row= $result->fetch_assoc()){
                    echo "<tr>";
                    echo "<td>".$row['memberID']."</td>";
                    echo "<td>".$row['MFirst']."</td>";
                    echo "<td>".$row['MLast']."</td>";    
                    echo "<td>".$row['Street']."</td>"; 
                    echo "<td>".$row['number']."</td>"; 
                    echo "<td>".$row['postalCode']."</td>"; 
                    echo "<td>".$row['Mbirthdate']."</td>"; 
                    echo "<td><a class =\"edit\" href=\"\">Edit </a>
                     | <a href=\"deletefmtable.php?memberID=$row[memberID]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";  
                }
                echo "</TABLE>";
            }
            else { echo "0 result"; }
            $conn->close();
         ?>
    </TABLE>
    <div id="dialog-form" title="Create new user" style="display:none">

        <p class="validateTips">

            All form fields are required.</p>       

            <form>

        <fieldset>

            <label for="memberID">

                Member ID </label>

                <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />

            <label for="MFirst">

                First Name</label>

                <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />


            <label for="MLast">

                Last Name </label>

            <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />

            <label for="Street">

                Street </label>

            <input type="text" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />

            <label for="number">

             number </label>

            <input type="text" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />

            <label for="postalCode">

            Postal Code </label>

            <input type="text" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />

            <label for="Mbirthdate">

            Birthdate </label>

            <input type="text" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />

        </fieldset>

        </form>

    </div>
</BODY>
</HTML>

图书馆数据库
$(函数(){
var new_dialog=函数(类型,行){
var dlg=$(“#对话框形式”).clone();
var memberID=dlg.find((“#memberID”),
MFirst=dlg.find((“#MFirst”),
MLast=dlg.find((“#MLast”),
Street=dlg.find((“#Street”),
number=dlg.find((“#number”),
postalCode=dlg.find((“#postalCode”),
Mbirthdate=dlg.find(“#Mbirthdate”);
类型=类型| |“创建”;
变量配置={
自动打开:对,
身高:500,
宽度:600,
莫代尔:是的,
按钮:{
“创建帐户”:保存数据,
“取消”:函数(){
dlg.对话框(“关闭”);
}
},
关闭:函数(){
dlg.remove();
}
};
如果(类型=='Edit'){
config.title=“编辑用户”;
获取_数据();
删除(配置按钮['createanaccount']);
配置按钮['Edit account']=函数(){
window.open(“edituple.php?memberID=“+memberID+”&MFirst=“+MFirst+”&MLast=“+MLast+”&Street=“+Street
+“&number=“+number+”&postalCode=“+postalCode+”&Mbirthdate=“+Mbirthdate”);
dlg.对话框(“关闭”);
}; 
}
对话框(配置);
函数get_data(){
var_memberID=$(row.children().get(1)).text(),
_MFirst=$(row.children().get(2)).text(),
_MLast=$(row.children().get(3)).text(),
_Street=$(row.children().get(4)).text(),
_number=$(row.children().get(5)).text(),
_postalCode=$(row.children().get(6)).text(),
_Mbirthdate=$(row.children().get(7)).text();
memberID.val(_memberID);
MFirst.val(_MFirst);
最后一个值(MLast);
街;瓦尔街(u街);;
编号.val(_编号);
postalCode.val(_postalCode);
Mbirthdate.val(_Mbirthdate);
} 
函数save_data(){
$(“#users tbody”).append(“+”+(fname.find(“option:selected”).text()+).concat(lname.find(“option:selected”).text()+”+“+”+email.val()++“+”+password.val()+”);
dlg.对话框(“关闭”);
}
}; 
$(文档).on('click','span.delete',函数(){
$(this).最近('tr').find('td').fadeOut(1000,
函数(){
//警报($(this.text());
$(this.parents('tr:first').remove();
}); 
返回false;
});
$(文档)。在('click','td a.edit',函数(){
新建_对话框('Edit',$(this).parents('tr');
返回false;
}); 
$(“#创建用户”)。按钮()。单击(新建对话框);
});


memberID 名字 姓 街头 数 邮政编码 生日 更新

所有表单字段都是必填项。

成员ID 名字 姓 街头 数 邮政编码 生日

代码的目的是显示一个表,并在旁边显示“编辑”和“删除”。Delete按钮工作得很好,但是对于edit,我需要将上面提到的变量传递到一个php文件

在发送之前使用JavaScript的JSON.stringify(您的变量)函数,在php端,使用JSON_decode($\u GET['data'])

您的_变量应该是一个对象,您不应该像代码一样包装它们。这样做:

data = {
    var _memberID = $(row.children().get(1)).text(),
     _MFirst = $(row.children().get(2)).text(),
     _MLast = $(row.children().get(3)).text(),
    _Street = $(row.children().get(4)).text(),
    _number = $(row.children().get(5)).text(),
    _postalCode = $(row.children().get(6)).text(),
    _Mbirthdate = $(row.children().get(7)).text();
    memberID.val(_memberID);
    MFirst.val(_MFirst);
    MLast.val(_MLast); 
    Street.val(_Street);
    number.val(_number); 
    postalCode.val(_postalCode);
    Mbirthdate.val(_Mbirthdate); 
}

然后通过ajax post将这些数据(字符串或标题设置为content type:application/json)传递给php,在php端,如我所述,对发布的数据进行json_解码,您将有一个数据数组用于sql查询。

在发送之前使用JavaScript的json.stringify(您的_变量)函数,在php端,使用json_解码($\u GET['data'])

您的_变量应该是一个对象,您不应该像代码一样包装它们。这样做:

data = {
    var _memberID = $(row.children().get(1)).text(),
     _MFirst = $(row.children().get(2)).text(),
     _MLast = $(row.children().get(3)).text(),
    _Street = $(row.children().get(4)).text(),
    _number = $(row.children().get(5)).text(),
    _postalCode = $(row.children().get(6)).text(),
    _Mbirthdate = $(row.children().get(7)).text();
    memberID.val(_memberID);
    MFirst.val(_MFirst);
    MLast.val(_MLast); 
    Street.val(_Street);
    number.val(_number); 
    postalCode.val(_postalCode);
    Mbirthdate.val(_Mbirthdate); 
}

然后通过ajax post将这些数据(字符串或标题设置为content-type:application/json)传递给php,在php端json_解码我上面提到的发布数据,您将有一个数据数组用于sql查询。

好的,我喜欢挑战。从顶部开始:

  • 您希望尽可能避免混合使用PHP和HTML。JavaScript也是如此。理想情况下,它们完全在不同的文件中,但我们将在这里将它们分开。在顶部工作,限制自己只显示下面的代码