简体   繁体   English

将可变数据从Javascript传递到PHP文件

[英]Pass variable data from Javascript to PHP file

Is there a way for me to submit data collected and passed to a variable using javascript to a php page. 有没有一种方法可以将使用javascript收集并传递给变量的数据提交到php页面。 I have a form that has input fields that are posted to a php file I am using a jquery function to serialize the form data and process it in php. 我有一个表单,其中的输入字段发布到php文件中,我正在使用jquery函数序列化表单数据并在php中进行处理。 However as well as the form data from the input fields I want to send data that I have collected and stored in a javascript variable and pick that up into a php variable so I can run a for loop. 但是,除了输入字段中的表单数据之外,我还想发送已收集并存储在javascript变量中的数据,并将其收集到php变量中,这样我就可以运行for循环。

 <!doctype html> <html> <head> <meta charset="UTF-8"> <title>New User Registration</title> </head> <body> <div id="newUserHld"> <table id="newUserFrmTbl"> <form id="newUserFrm"> <tr> <td><label id="firstnameLbl">First Name</label></td> <td><input type="text" id="firstname" size="20" /></td> </tr> <tr> <td><label id="lastnameLbl">Last Name</label></td> <td><input type="text" id="lastname" size="20" /></td> </tr> <tr> <td><label id="dobLbl">Date of Birth</label></td> <td><input type="text" id="dob" size="8" /></td> </tr> <tr> <td><label id="positionLbl">Position</label></td> <td> <select size="1"> <option>Select Option</option> <option> WFXM </option> <option>Operations Manager</option> <option>Assistant</option> </select> </td> </tr> <tr> <td><label id="usernameLbl">Username</label></td> <td><input type="text" id="username" size="20" /></td> </tr> <tr> <td><label id="passwordLbl">Password</label></td> <td><input type="password" id="password" size="20" /></td> </tr> <tr> <td><label id="payRateLbl">Pay Rate</label></td> <td><input type="text" id="payRate" size="20" /></td> </tr> <tr><td><input type="submit" id="createUsr" name="createUsr" value="Create User" /> </td> </form> </table> </div> <div id="success"> </div> <button type="button" onclick="getFrmElm()">Try it</button> </body> <script> function getFrmElm (){ var x = document.getElementById("newUserFrm"); var txt = ""; var i; for (i = 0; i < x.length; i++) { txt = txt + x.elements[i].id + "<br>"; } formSubmit(); } function formSubmit(){ $.ajax({ type:'POST', url:'userreg.php', data:$('#newUserFrm').serialize(), success:function(response){ $('#success').html(response); } }); return false; } </script> </html> PHP FROM userreg <?php include_once "iud.php"; $db = connectDatabase(); $formfields = test_input($_POST['txt']; $firstname=test_input($_POST['firstname']); $lastname=test_input($_POST['lastname']); $sql="INSERT INTO contacts(firstname, lastname) "; $sql .= "values ("; $sql .= "'".$firstname."', '".$lastname."'"; $sql .= ")"; if(mysqli_query($db, $sql)){ echo "Record Inserted"; }else{ echo "Insert failed"; } mysqli_close($db); function test_input($data){ $data=trim($data); $data=stripslashes($data); $data=htmlspecialchars($data); return $data; } ?> 

Basically I want to send the data from the form but also from the VAR txt with it. 基本上,我想从表单发送数据,也要从VAR txt发送数据。

Modify your ajax to contain data Key. 修改ajax以包含数据密钥。 Currently there are no key there. 目前那里没有钥匙。

$.ajax({
    type:'POST',
    url:'userreg.php',
    data: {
        firstName: $("#firstName").val(),
        lastName: $("#lastName").val(),
        // ... and so on until all the form element in here - then your PHP will work
    },
    success:function(response){
        $('#success').html(response);
    }
});

在此处输入图片说明 your missing name attribute in input field and if u user ajax need to link jquery library file in header. 您在输入字段中缺少的名称属性,以及您的用户ajax是否需要在标头中链接jquery库文件。 below code help to post your form data using ajax. 下面的代码有助于使用ajax发布表单数据。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>New User Registration</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
    <div id="newUserHld">
        <table id="newUserFrmTbl">
            <form id="newUserFrm" method="POST" >
                <tr>
                    <td><label id="firstnameLbl">First Name</label></td>
                    <td><input type="text" id="firstname" size="20" name="firstname" /></td>
                </tr>
                <tr>
                    <td><label id="lastnameLbl">Last Name</label></td>
                    <td><input type="text" id="lastname" size="20" name="lastname" /></td>
                </tr>
                <tr>
                    <td><label id="dobLbl">Date of Birth</label></td>
                    <td><input type="text" id="dob" size="8" name="dob" /></td>
                </tr>
                <tr>
                    <td><label id="positionLbl">Position</label></td>
                    <td>
                        <select size="1" name="positionLbl">
                            <option>Select Option</option>
                            <option> WFXM </option>
                            <option>Operations Manager</option>
                            <option>Assistant</option>

                        </select>
                    </td>
                </tr>
                <tr>
                    <td><label id="usernameLbl">Username</label></td>
                    <td><input type="text" id="username" size="20" name="username" /></td>
                </tr>
                <tr>
                    <td><label id="passwordLbl">Password</label></td>
                    <td><input type="password" id="password" size="20"  name="password" /></td>
                </tr>
                <tr>
                    <td><label id="payRateLbl">Pay Rate</label></td>
                    <td><input type="text" id="payRate" size="20"  name="payRate" /></td>
                </tr>
                <tr><td><input type="submit" id="createUsr" name="createUsr" value="Create User"  onclick="formSubmit(); return false;" />  </td>   
            </form>
        </table>
    </div>
    <div id="success">
    </div>
</body>
    <script>

function formSubmit(){
    alert($('#newUserFrm').serialize());
            $.ajax({
                type:'POST',
                url:'userreg.php',
                data:$('#newUserFrm').serialize(),
                    success:function(response){
                        $('#success').html(response);
                    }
                });



            return false;
        }


    </script>
</html>

and php File to check response. 和php文件检查响应。

<?php 
print_r($_POST);
exit;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM