传递当前页面's的php变量传递到XMLHttpRequest2(即$user_id)
Pass a current page's php variable through to XMLHttpRequest2 (i.e. $user_id)
我正试图弄清楚当前页面的php$var是否可以传递到XMLHttpRequest2。正在调用的文件位于/assets/js目录中的视图(当前php页面所在的位置)文件夹之外。我也在使用CodeIgniter。试图将$user_id传递给XMLHttpRequest2请求文件中的SQL查询。
publication_call.php(当前文件)
<form>
<input type="hidden" id="someid" value="<?= $idz ?>"/>
<?php
echo form_label('Validation: (Enter Publication keywords, Matches will appear in Dropdown > )');
echo form_label('Matching<br>Publications:');
?>
<select name="matched_pub" id="matched_pub"></select>
</form>
<script>
jQuery(function($){
//still want to bind the change event
$('#matched_pub').bind('change', function(){
$('#title').val($('#matched_pub option:selected').text());
});
$('#validation').keyup(function() {
showKeywords( $('#validation').val() );
document.getElementById('matched_pub').style.display='block';
});
});
</script>
<script>
function showKeywords(str)
{
if (document.getElementById("matched_pub")) {
if (str.length==0)
{
document.getElementById("matched_pub").innerHTML="";
document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText;
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText;
}
}
xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str,true);
xmlhttp2.send();
}
}
</script>
searchwords.php(请求/外部文件)
<?php
$user = 'root';
$pass = 'root';
$db = 'hey_there';
$host = 'localhost';
$conn = mysql_connect($host, $user, $pass);
$db_selected = mysql_select_db($db, $conn);
//trying to display special chars
mysql_query("set names 'utf8'");
if(!$db_selected) {
echo 'broke';
}
//echo 'db connected';
$q = $_GET["b"];
//explode and parse $q into all the fragments separated by spaces and do full text search +word1 +word2 +word3, this will ignore HTML tags as it ignores word order, will also solve the middle initial problem [db setup is not compatible with full text search, but can do likes per word, less efficient, but how it must be done]
$queryWords = explode(' ', $q);
//for services query, explode the query into words and search for each separately
$query = "SELECT DISTINCT(pub_title)
FROM teacher_publications
JOIN users ON teacher_publications.user_id = users.id
WHERE keywords IS NOT NULL
AND pub_title IS NOT NULL
AND teacher_publications.user_id = 103 <-- $var will go here
";
$queryServicesLoop = '';
$queryServicesEnd = ' ORDER BY pub_title ASC';
//loop through all words in string
foreach($queryWords as $queryWord) {
$queryServicesLoop .= " AND (keywords LIKE '%{$queryWord}%')";
}
$queryServices = $queryServices.$queryServicesLoop;
$queryServices = $queryServices.$queryServicesEnd;
$resultServices = mysql_query($queryServices);
$services ='';
if(mysql_num_rows($resultServices) > 0){
while($rowServices = mysql_fetch_assoc($resultServices)) {
$services .= '<option value="' . $rowServices['pub_title'] . '">' . $rowServices['pub_title'] . '</option>';
}
}
if( mysql_num_rows($resultServices) == 0 )
{
echo '<option value="">Your search failed to find any matching results.</option>';
}
else
{
echo '' . $services . '';
}
/*===================================已编辑代码====================================*/
publication_call.php(当前文件)
<input type="hidden" id="someid" value="<?= $user_id ?>"/>
<script>
function showKeywords(str)
{
if (document.getElementById("matched_pub")) {
if (str.length==0)
{
document.getElementById("someid");
document.getElementById("matched_pub").innerHTML="";
document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText;
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText;
}
}
xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str+"&user_id="+document.getElementById('someid'), true);
// xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str,true);
xmlhttp2.send();
}
}
</script>
searchwords.php(请求/外部文件)
$usr = $_GET["user_id"];
$query = "SELECT DISTINCT(pub_title)
FROM teacher_publications
JOIN users ON teacher_publications.user_id = users.id
WHERE keywords IS NOT NULL
AND pub_title IS NOT NULL
AND teacher_publications.user_id = ".$usr."
";
您可以将$user_id
放入隐藏的输入字段中,并使用Javascript读取它的值以用于Ajax请求
你可以这样做:
<input type="hidden" id="someid" value="<?= $user_id ?>
然后在你完成之后,你可以通过这样做来获得价值:
如果使用jquery ,则使用纯Javascript的document.getElementById('someid');
或$('#someid').value();
这将为您获取用户ID值,然后您可以在请求中使用该值。
像这样:
xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str+"&user_id="+document.getElementById('someid').value, true);
将当前的xmlhttp2.open替换为上面的现在,您可以访问请求文件中$_GET['user_id']
中的用户ID值。
- 解析-为什么user.getSessionToken()返回未定义的结果
- 我将如何将Base的原型分配给User
- PassportJS/忘记密码:req.user只定义一次
- User getSessionToken() Returning Undefined
- 在 Meteor.loginWithPassword 之后,旧用户不会离线(meteor-user-status 包)
- 为什么Parse.User.current()为null
- 在/user/后面显示用户名的用户信息
- AngularJS和Spring后端-在AngularJS中以user.password的形式从数据库中获取密码
- req.user在使用express服务器passport.js进行twitter身份验证后未定义
- 希望passport存储req.client,而不是用于承载策略的req.user
- document.getElementById(“#”).innerHTML=“”;你好"+user+&quo
- 无法使用快递和猫鼬进行路由,请继续获取"类型错误:req.params.user不是函数;
- 正则表达式 path.match(/user/);.
- 在Safari浏览器上,User.Identity.Name为空
- WordPress Ajax Call -- WordPress User ID
- Google App Engine: User is null
- express-jwt 无法从路由目录访问 req.user
- Meteor.user() 错误:未捕获类型错误:无法读取 null 的属性“name”
- -moz-user-select in Javascript
- Laravel4 文件上传通过 XMLHttpRequest2.