簡體   English   中英

Js,Ajax,Json-如何不從自動完成功能將“ Array”插入數據庫

[英]Js, Ajax, Json - How not to insert “Array” into db from autocomplete

我建立了一個小腳本,它將根據第一個輸入字段的自動完成功能自動填充幾個輸入字段。

我的腳本正常工作,但是當我按下“提交”按鈕時,一切似乎都還可以,但是當我查看數據庫以查看插入的內容時,所有字段都以“ Array”插入,而不是應該插入的內容。 在自動完成功能上,我可以看到該字段是具有正確信息的字段,但是我想php無法理解它是什么,而是插入“ Array”。

任何想法如何解決?

我的表格:

<form action="job_post.php" method="post">
                    <div class="form-group has-success col-md-3">
                    <label class="control-label" for="inputSuccess1">First Name: </label>
                    <input type='text' id='firstname' name='firstname[]'/>
                </div>
                <div class="form-group has-success col-md-3">
                    <label class="control-label" for="inputSuccess1">Last Name: </label>
                    <input type='text' id='lastname' name='lastname[]'/>
                </div>
                <div class="form-group has-success col-md-3">
                    <label class="control-label" for="inputSuccess1">Age: </label>
                    <input type='text' id='age' name='age[]'/>
                </div>
                <div class="form-group has-success col-md-3">
                    <label class="control-label" for="inputSuccess1">Company Name: </label>
                    <input type='text' id='CoName' name='CoName[]'/>
                </div>

<input type="submit">
</form>

我的job_post.php

<?php

require_once 'db_connect.php';

$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$age=$_POST['age'];
$CoName=$_POST['CoName'];

$qry=mysql_query("INSERT INTO jobs (FirstName, LastName, Age, CoName)
VALUES ('$firstname', '$lastname', '$age', '$CoName')", $con);
if(!$qry)
{
mysql_close($con);
die("Query Failed: ". mysql_error());
}

else
{
mysql_close($con);
header("Location:index.php");
}

mysqli_close($con);
?>

我的Js:

$('#firstname').autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url : 'ajax.php',
                            dataType: "json",
                            data: {
                               name_startsWith: request.term,
                               type: 'firstname',
                               row_num : 1
                            },
                             success: function( data ) {
                                 response( $.map( data, function( item ) {
                                    var code = item.split("|");
                                    return {
                                        label: code[0],
                                        value: code[0],
                                        data : item
                                    }
                                }));
                            }
                        });
                    },
                    autoFocus: true,            
                    minLength: 2,
                    select: function( event, ui ) {
                        var names = ui.item.data.split("|");
                        console.log(names[1], names[2], names[3]);                      
                        $('#lastname').val(names[1]);
                        $('#age').val(names[2]);
                        $('#CoName').val(names[3]);
                    }               
                  });

我的Ajax:

<?php
/*
Site : http:www.smarttutorials.net
Author :muni
*/
require_once 'db_connect.php';

if($_GET['type'] == 'firstname'){
    $row_num = $_GET['row_num'];
    $result = mysql_query("SELECT FirstName, LastName, Age, CoName FROM jobs where FirstName LIKE '".strtoupper($_GET['name_startsWith'])."%'");    
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        $name = $row['FirstName'].'|'.$row['LastName'].'|'.$row['Age'].'|'.$row['CoName'].'|'.$row_num;
        array_push($data, $name);   
    }   
    echo json_encode($data);
}


?>

它確實提取數據,但是我想通過提交按鈕將其插入數據庫。

任何幫助,高度贊賞。

更新信息

然后將變量放入您現在的查詢中。

頁面重做:

 <?php

    require_once 'db_connect.php';

    $firstname = $_POST["firstname"][0];
    $CoName = $_POST["CoName"][0]; 

    /*
     * Last Name and Age are not in the array, I removed them from the query 
     * however you need to place them in the array if you wish to add them
     */

    $qry=mysql_query("INSERT INTO jobs (FirstName, CoName)
        VALUES ('$firstname', '$CoName')", $con);

    if(!$qry) {
        mysql_close($con);
        die("Query Failed: ". mysql_error());
    } else {
        mysql_close($con);
        header("Location: index.php");
    }

    ?>

再次更新,誤讀了數組。

確保您正在獲取單條記錄,因為此使用限制為sql中的1(不好的主意)。 否則循環返回數組以進行多次插入。 插入之前使用print_r($_POST); 並查看您要插入的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM