簡體   English   中英

JS將變量傳遞給.php,基於變量的php返回,由javascript顯示,使用AJAX和具有bind('input',function()的觸發器

[英]JS passing variable to .php, php returning based on variable, displayed by javascript, using AJAX and triggers with bind('input', function ()

我正在嘗試使表單自動填寫一些輸入字段onchange,我似乎無法弄清楚如何將#url字段發送到我的php腳本,非常歡迎任何輸入或建議。

$('#url').bind('input', function () {});

在keychange上將輸入id $ url的值發送到ajax.php

這將以該值運行

$query = "SELECT * FROM `inserted_posts` WHERE `search_name` = '$url'";

結果回顯

echo json_encode($array);

然后顯示在其他輸入字段中

           $.ajax({
            url: 'ajax.php',   
            data: "", 

            dataType: 'json', //data format      
            success: function (data) //on recieve of reply
            {
                var catagoryPHP = data[1]; 
                var imageNamePHP = data[3];

                $('#catagory').html(catagoryPHP);
                $('#imageName').html(imageNamePHP);
            }
            });

我似乎無法弄清楚如何將$ URL發送到ajax.php並啟動事件鏈?

我所有的代碼

JS:

$(function () {
$('#url').bind('input', function () {
    $(this).val() // get  value




    $(function () {
        //-----------------------------------------------------------------------
        // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
        //-----------------------------------------------------------------------
        $.ajax({
            url: 'ajax.php', //the script to call to get data          
            data: "", //you can insert url argumnets here to pass to api.php
            //for example "id=5&parent=6"
            dataType: 'json', //data format      
            success: function (data) //on recieve of reply
            {
                var catagoryPHP = data[1]; 
                var imageNamePHP = data[3];

                //--------------------------------------------------------------------
                // 3) Update html content
                //--------------------------------------------------------------------
                $('#catagory').html(catagoryPHP);
                $('#imageName').html(imageNamePHP);

            }
        });
    });
});
});

ajax.php

<?php
require_once ('DBconnect.php');

$query = "SELECT * FROM `inserted_posts` WHERE `search_name` = '$url'";
$result = mysql_query($query);

$array = mysql_fetch_array($result);

echo json_encode($array);
?>
    $.ajax({
        type: 'POST', // Can be 'GET'
        url: 'ajax.php',      
        data: { url: $('#url').val() },         
        success: function (data) //on recieve of reply
        {
            /* Your code to execute on success of the request */
        }
    });

#url將在您的php腳本中以$ _POST ['url']或$ _GET ['url']的形式訪問,具體取決於您為請求type

請記住使用預處理語句或mysqli_escape_string()函數mysqli_escape_string()所有用戶輸入。

暫無
暫無

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

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