【发布时间】:2013-12-01 23:24:43
【问题描述】:
我正在编写一个脚本,该脚本根据所选值回显答案。稍后这将是多个值,但现在我只是在测试。 php 脚本通过 AJAX 调用,因此结果将显示在同一页面上。
唯一的问题是在这种情况下我的变量 $brand 没有传递,因此脚本将始终返回我的 else 语句。我已将变量包含在 echo 中以检查它是否已通过,但事实并非如此。这里出了什么问题?
这是我的索引文件代码:
<?php
session_start();
?>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="JS/jquery-1.10.2.js" ></script>
<script>
function myCall(){
var request = $.ajax({
url: "process.php",
type: "post",
dataType: "html"
});
request.done(function(msg) {
$("#response").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
</head>
<body>
<div id="formulier">
<form method="" action="" name="form" id="form">
<label for="brand">Brand</label>
<select id="brand" name="brand">
<option value="" >- Select -</option>
<option value="1">Marlboro (1)</option>
<option value="2">Pall Mall (2)</option>
</select>
<input type="button" value="submit" onclick="myCall();">
</form>
</div>
<div id="response">
</div>
这是我的 php 文件(process.php):
<?php
session_start();
$brand = $_POST['brand'];
if( $brand == '1'){
echo "Value is 1";
}
else{
echo "Value is: ".$brand;
}
?>
【问题讨论】:
-
我没有看到对
$_SESSION的任何引用?假设您只是在这里谈论发布数据... -
在您的 jQuery ajax 请求中,我没有看到任何正在发送的数据。你指定了一个数据类型,但从未说过要发送什么。
标签: php jquery ajax session variables