dongyan2445 2015-05-01 02:11
浏览 60
已采纳

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

So I'm trying to make a basic mockup shoe store for one of my classes, but I've been looking for a way to take a variable in the url and send it to my PHP...

This is my 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'];
}


?>

and I was trying to get it to work with JavaScript/jQuery/ajax, but I couldn't find a method to get model_id (which is in the form of a setup) and pass it back to the 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>

Example of my tag:

<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: This is all in the same file..

EDIT:

Old PHP loop -

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

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'];
    }
}
  • 写回答

2条回答 默认 最新

  • doutandusegang2961 2015-05-19 22:06
    关注

    Here is what I've actually come up with, this is the final version that works... Thank you for your help, but I had to dig deeper to figure it out.

    <script>
    
            function parseData(html) {
                var json = {};
                $.each(document.getElementsByTagName("div"), function(index, value) {
                    json[value.id] = value.innerHTML;
                });
                return json;
            };
    
             $("a").click(function(e) {
                e.preventDefault();
                var href = $(this).attr("href");
                history.pushState({}, '', $(this).attr("href"));
                $.get("/~huntcki3/test.php"+$(this).attr("href"), function(data) {
                    var info = JSON.parse(data);
                    console.log(info);
                    console.log(href);
                    $("div.og-details h3")[0].innerHTML = info.name;
                    $("div.og-details p")[0].innerHTML = info.style+'<br>' + 'Size: ' + info.size+'<br>' + '$' + info.price+' USD';
                });
            });
            </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化