変数 $id=$_GET["categoryID"] を設定しようとしています。私はそれを働かせることができません。Ajaxリクエストに関係していると思います。しかし、そのリクエストと連携して機能するようにフォーマットする方法がわかりません。mysql クエリの変数が必要です。どんな助けでも大歓迎です。これは私の頭の中にあり、何日も苦労しています。GET と POST の両方を試しました。ありがとう。
私はこれまでページを要約しました...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test $_GET</title>
</head>
<body>
<?php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$id = $_GET["categoryID"];
//$id=3;
}
?>
print_r($_GET) = <?php print_r($_GET); ?>
<br />
print_r($id) = <?php print_r($id); ?>
</body>
</html>
これが結果のページです....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test $_GET</title>
</head>
<body>
print_r($_GET) = Array
(
[categoryID] => 1001
)
<br />
print_r($id) =
</body>
</html>
これが全ページです....
<?php
if (@$_REQUEST['ajax']) {
$link = $nm33;
if ($link == false)
trigger_error('Connect failed - ' . mysql_error(), E_USER_ERROR);
$connected = mysql_select_db('nm', $link);
if ($connected) {
//How do I set $id = $_GET["categoryID"] It fails to set the variable.
$id =$_GET["categoryID"];
// $id=1;
// It will work as $id=1
$results = mysql_query('select * from selectMenu where categoryID= \'' . $id . '\' AND category="' . strtolower(mysql_real_escape_string(strip_tags($_REQUEST['category']))) . '"');
//////////
$json = array();
while (is_resource($results) && $row = mysql_fetch_object($results)) {
//$json[] = '{"id" : "' . $row->id . '", "label" : "' . $row->label . '"}';
$json[] = '"' . $row->label . '"';
}
echo '[' . implode(',', $json) . ']';
die(); // filthy exit, but does fine for our example.
} else {
user_error("Failed to select the database");
}
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="js/select-chain.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
<!--
$(function () {
var cat = $('#categorySelect');
var el = $('#elementSelect');
var attr = $('#attributeSelect');
el.selectChain({
target: attr,
url: 'select-menu.php',
data: { ajax: true, anotherval: "anotherAction" }
});
// note that we're assigning in reverse order
// to allow the chaining change trigger to work
cat.selectChain({
target: el,
url: 'select-menu.php',
data: { ajax: true }
}).trigger('change');
});
//-->
</script>
<link href="selectMenu.css" rel="stylesheet" type="text/css" />
<form action="performance-models.php" method="get">
<select name="category" class="dropdown" id="categorySelect">
<option selected="selected">Select Your Vehicle</option>
<?php do { ?>
<option> <?php echo $row_rsMake['make']; ?></option>
<?php } while ($row_rsMake = mysql_fetch_assoc($rsMake)); ?>
</select>
<select name="model" class="dropdown" id="elementSelect">
<option selected="selected">Select Model</option>
<option>[none selected]</option>
</select>
<select name="appYear" class="dropdown" id="attributeSelect" >
<option selected="selected"> </option>
<option>[none selected]</option>
</select>
<input type="submit" value="Go">
</form>
<p><br />
<br />
print_r($_GET) = <?php print_r($_GET); ?> <br />
print_r($_REQUEST) = <?php print_r($_REQUEST); ?><br />
echo $_REQUEST['categoryID'] <?php echo $_REQUEST['categoryID'];?>
</p>
これがselect-chain.jsです
(function ($) {
$.fn.selectChain = function (options) {
var defaults = {
key: "id",
value: "label"
};
var settings = $.extend({}, defaults, options);
if (!(settings.target instanceof $)) settings.target = $(settings.target);
return this.each(function () {
var $$ = $(this);
$$.change(function () {
var data = null;
if (typeof settings.data == 'string') {
data = settings.data + '&' + this.name + '=' + $$.val();
} else if (typeof settings.data == 'object') {
data = settings.data;
data['category'] = $$.val();
data['model'] = $$.val();
data['year'] = $$.val();
}
settings.target.empty();
$.ajax({
url: settings.url,
data: data,
type: (settings.type || 'get'),
dataType: 'json',
success: function (j) {
var options = [], i = 0, o = null;
for (i = 0; i < j.length; i++) {
// required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
o = document.createElement("OPTION");
o.value = typeof j[i] == 'object' ? j[i][settings.key] : j[i];
o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i];
settings.target.get(0).options[i] = o;
}
// hand control back to browser for a moment
setTimeout(function () {
settings.target
.find('option:first')
.attr('selected', 'selected')
.parent('select')
.trigger('change');
}, 0);
},
error: function (xhr, desc, er) {
// add whatever debug you want here.
alert("an error occurred here");
}
});
});
});
};
})(jQuery);