javascript - 需要一种使用 jQuery 将 URL 变量发送到 PHP 的方法

标签 javascript php jquery ajax

所以我试图为我的一个类(class)制作一个基本的模型鞋店,但我一直在寻找一种方法来获取网址中的变量并将其发送到我的 PHP...

这是我的 php:

<?php
// This block allows our program to access the MySQL database.
// Stores your login information in PHP variables
require_once 'studentdb.php';
// Accesses the login information to connect to the MySQL server using your credentials and database
$db_server = mysql_connect($host, $username, $password);
// This provides the error message that will appear if your credentials or database are invalid
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($dbname)
    or die("Unable to select database: " . mysql_error());

if(isset($_GET['model_id']) && is_generic($_GET['model_id'])) {
    $model_id = $_GET['model_id'];
    $result = mysql_query('CALL shoes(`'.$model_id.'`);');
    $row=mysql_fetch_array($result);
    echo $row['size'];
}


?>

我试图让它与 JavaScript/jQuery/ajax 一起工作,但我找不到一种方法来获取 model_id (以设置的形式)并将其传递回 PHP。

<script>
 $("a").click(function(e) {
    e.preventDefault;
    var shoePrice = $(this).attr('href');
    history.pushState({}, '', $(this).attr("href"));
    $("a").attr('data-title', shoePrice);
    return false;
  });
</script>

我的标签示例:

<a href="?model_id=1" data-largesrc="images/shoe1.jpg" data-title="Test" data-description="Shoe description here" data-price="Price here"><img src="images/thumbs/shoe1.jpg" alt="img01"/>

PS:这一切都在同一个文件中..

编辑:

旧的 PHP 循环 -

$model_id = isset($_GET['model_id']) ? $_GET['model_id'] : 0;
if($_GET["model_id"] === "") echo "model_id is an empty string \n";
if($_GET["model_id"] === false) echo "model_id is false \n";
if($_GET["model_id"] === null) echo "model_id is null \n";
if(isset($_GET["model_id"])) echo "model_id is set \n";
if(!empty($_GET["model_id"])) echo "model_id is not empty \n";

if(isset($model_id)) {
    $query = 'SELECT size, price, style FROM shoes WHERE model_id='.$model_id;
    $search1 = 'SELECT * FROM shoes WHERE model_id='.$model_id;
    $abc = mysql_query($search1);
    $result = mysql_query($query);
    // The mysql_num_rows function returns an integer representation of number of rows for the table passed as an argument
    $number_of_requests = mysql_num_rows($result);
    if(! $result) {
        die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo    "Shoe ID: {$row['model_id']} <br>".
                "Shoes Size: {$row['size']}<br>".
                "Shoe Price: {$row['price']}<br>".
                "Shoes Style: {$row['style']}<br>";


    }
    while($row = mysql_fetch_assoc($abc)) {
        $size = $row['size'];
    }
}

最佳答案

假设您将请求发送到同一页面,因为 href 显示您所需要的只是点击处理程序中的 $.get

$("a").click(function(e) {
    e.preventDefault;
    var shoePrice = $(this).attr('href');
    history.pushState({}, '', $(this).attr("href"));
    $("a").attr('data-title', shoePrice);
    $.get(shoePrice , function(serverResponse){
       // do something here with response
    })
});

$,get$.ajax 的简写方法。

如果您接收的是 json 可以将 $.get 替换为 $.getJSON

关于javascript - 需要一种使用 jQuery 将 URL 变量发送到 PHP 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980701/

相关文章:

javascript - jQuery preventDefault 不表现

javascript insidehtml 没有响应

php - 更改两个域的根目录

php - 在 php 中从中心自动裁剪 Tumble 的照片

jquery - 使用 jquery/css 在背景中显示图像

javascript - Ember JS #each 循环的值必须是数组

javascript - 谷歌地图API搜索特定国家的邮政编码

PHP 注意,返回 "array array array"而不是值

jQuery 路点和响应式代码不能很好地发挥作用

javascript - 仅使用一个js文件进行多个php ajax调用