gpt4 book ai didi

php - 如何在html中将javascript变量传递给php? (维基百科解析)

转载 作者:行者123 更新时间:2023-11-28 09:07:26 26 4
gpt4 key购买 nike

嗨,我正在尝试将一个名为 $url 的 JavaScript 变量传递给 php 中的一个函数,所有函数都位于同一个 html 文件中。这里的逻辑是,有一个文本框,用户输入一个物种,我们调用维基百科 api 来检查输入是否有维基百科页面,如果有,我解析该页面并显示物种信息,一切都 100% 独立工作但我似乎无法连接它们。但是我无法让 javascript 将 url 传递给下面的 php 函数。和innerHTML php函数与url,它应该呈现解析的物种信息。我得到的错误是,从一开始我就什么也没得到 php 函数。

Enter a species here : [text box here]
find('table.infobox'); foreach($html->find('img') as $element) { $image[]= $element; } Print $image[1]; $data = array(); foreach($table[0]->find('tr') as $row) { $td = $row->find('> td'); if (count($td) == 2) { $name = $td[0]->innertext; $td = $td[1]->find('a'); $text = $td[0]->innertext; $data[$name] = $text; } } print ""; foreach($data as $value => $before ) { print ""; } print "
$value $before
"; } ?>

这是我到目前为止的尝试,我调用 javascript 并检查是否找到链接,然后调用解析函数以在结果 div 中导出 innerthml

<?php
//call the javascript
//if link found
//send ajax url variable to php function
//ajax call the function innerhtml in the result div



//We check if we have the param within the page call
$theDeletenode = false;
if(isset($_POST['deletenode']))
$theDeletenode = $_POST['deletenode'];

if($theDeletenode)
{
//The param 'deletenode' is given, so we juste have to call the php function "parse", to return the value
parse($theDeletenode);
}else
{
// No parametre, so we echo the javascript, and the form (without the quote and \n, it's much better)
?>

这是我的 html 的开始,文本框连接到 javascript,该 javascript 调用维基百科 api 来检查输入是否有维基百科页面。

<html><head>
<script type="text/javascript">
// create and return an XMLHttpRequest object
function createRequest() {
var ajaxRequest; // the xmlrequest object
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}; // end of createRequest

var spellcheck = function(data){
var found=false;var url='';
var text=data[0];
if(text!=document.getElementById('spellcheckinput').value) return;
for(i=0;i<data[1].length;i++)
{
if(text.toLowerCase()==data[1][i].toLowerCase())
{found=true;
url='http://en.wikipedia.org/wiki/'+text;
document.getElementById('spellcheckresult').innerHTML='<b style=\"color:green\">Correct</b> - <a target=\"_top\" href=\"'+url+'\">link</a>';
}
}
if(!found)
document.getElementById('spellcheckresult').innerHTML='<b style=\"color:red\">Incorrect</b>';
};


var requestone = createRequest();

var getjs= function(value)
{
if(!value) return;
var url='http://en.wikipedia.org/w/api.php?action=opensearch&search='+value+'&format=json&callback=spellcheck'; // this is the variable I want to pass
document.getElementById('spellcheckresult').innerHTML='Checking ...';
var elem=document.createElement('script');
elem.setAttribute('src',url);
elem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(elem);

// Ajax call to this page, this time with the 'deletenode' parameter
var vars = "url=" + encodeURIComponent(url);
requestone.open("POST", "parser.php", true); // parser.php : the name of this current page
requestone.onreadystatechange = handleRequest; // function to handle the response
requestone.send(vars);

};

function handleRequest()
{
//here we handle the php page response by echoing de result of the php page (normally the result of the parse function)
try{
if(requestone.readyState == 4 && requestone.status == 200)
document.getElementById('resultdiv').innerHTML= requestone.responseText;
} catch (e) {
//Wrong server answer...
}
};
</script>
</head>
<body>

<form action="#" method="get" onsubmit="return false">
<p>Enter a species here : <input id="spellcheckinput" onkeyup="getjs (this.value);" type="text">
<span id="spellcheckresult"></span></p>
</form>
<div id=resultdiv></div>
</body>
</html>

这是我的解析函数,它接收一个名为 url 的变量,该变量是经过验证的维基百科页面,然后解析它以获取物种信息,经过测试并且它有效,但我无法将其与经过验证的维基百科 url 一起传递到 insidehtml 这个函数。

<?php 
}
function parse($url){
print $url;
$html = file_get_html('http://en.wikipedia.org/wiki/Beaver');
$table = $html->find('table.infobox');


foreach($html->find('img') as $element)
{
$image[]= $element;
}
Print $image[1];

$data = array();
foreach($table[0]->find('tr') as $row)
{
$td = $row->find('> td');
if (count($td) == 2)
{
$name = $td[0]->innertext;
$td = $td[1]->find('a');
$text = $td[0]->innertext;
$data[$name] = $text;

}
}
print "<table class='infobox' style='text-align: left; width: 200px; font-size: 100%'>";
foreach($data as $value => $before )
{

print "<tr><td>$value</td><td>$before</td></tr>";
}
print "</table>";

}

?>

最佳答案

这个问题有很多相关答案1 2 3 4

如果有帮助,这里有一些 JavaScript:

var form = document.createElement("form");
var input = document.createElement("input");
form.method="{post/get}";
form.action="{page}";
input.name="{name}";
input.value="{value}";
form.appendChild(input);
form.submit();

关于php - 如何在html中将javascript变量传递给php? (维基百科解析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com