[英]Don't know how to solve (Between Php and Javascript)
I don't know how to solve the variable $cat by following script. 我不知道如何通过以下脚本来解决变量$ cat 。
" text " variable from Form to javascript and pass to php function to be $Categories_name and to be $cat . 从表单到javascript的“ text ”变量,并传递给php函数,分别为$ Categories_name和$ cat 。
I already test the $cat variable, it is not "String", it is "object", I don't understand. 我已经测试了$ cat变量,它不是“ String”,它是“ object”,我听不懂。
But I need $cat to be "String". 但是我需要$ cat为“ String”。
when Test = "This is Cat" (3 words), 当Test =“这是猫”(3个字)时,
I test $cat by php str_word_count and the output is 1 (I need to correct answer 3); 我通过php str_word_count测试$ cat,输出为1(我需要更正答案3);
I test $cat by php var_dump and no output (I need to correct answer "String"). 我通过php var_dump测试$ cat,没有输出(我需要更正答案“ String”)。
<p id="CaTable"></p>
<script>
function CaFunction()
{
var text = document.getElementById("CategorySelect").value;
document.getElementById("CaTable").innerHTML = "<?php php_catable('" + text + "'); ?>";
}
</script>
<!-- Generate Table by php and MySQL-->
<?php
function php_catable($Categories_name)
{
$cat = $Categories_name;
.................
.................
$sql = "select * from table where xyz = '" .$cat. "'";
}
?>
You are confusing server side code and client side code. 您正在混淆服务器端代码和客户端代码。 Your php code live on the server and can only be executed on the server.
您的php代码位于服务器上,并且只能在服务器上执行。 And your javascript is on your client's browser and does not know about the server (remember, php generate a text file, and only that text file is sent to the browser).
而且您的JavaScript位于客户端的浏览器上,并且不了解服务器(请记住,php会生成一个文本文件,只有该文本文件才会发送到浏览器)。 if you want to use the
php_catable()
function from your client, you will need to do an AJAX call or to redesign your page to do a form submit (just like what Steve is proposing). 如果要从客户端使用
php_catable()
函数,则需要进行AJAX调用或重新设计页面以进行表单提交(就像Steve提议的那样)。
Your First Page: Assuming CategorySelect
is a dropdown select box, create a script for its onChange event and create a method="post"
post form with a hidden input that goes to "generate_table.php". 您的第一页:假设
CategorySelect
是一个下拉选择框,为其onChange事件创建一个脚本,并创建一个带有隐藏输入的method="post"
发布表单,该表单的输入将进入“ generate_table.php”。
<input type="hidden" name="ca_table" id="ca_table" />
You make ca_table a hidden input so php will pick up the value from it when this page gets submitted to a second page where you can generate your table using the php function. 您将ca_table设为隐藏的输入,因此当此页面提交到第二页时,php将从中获取值,在第二页中您可以使用php函数生成表格。
<script language="javascript" type=text/javascript>
function CaFunction(){
documentGetElementById('ca_table').value = documentGetElementById('CategorySelect').value;
submit();
}
</script>
add this to your select dropdown: 将此添加到您的选择下拉列表中:
onChange="CaFunction();"
Your Receiving Page: So your receiving page "generate_table.php" would have 您的接收页面:因此,您的接收页面“ generate_table.php”将具有
<?php
function php_catable($Categories_name)
{
$cat = $Categories_name;
.................
.................
$sql = "select * from table where xyz = '" .$cat. "'";
}
$category_name = $_POST['ca_table']; // cleaned up at least with suitable preg_replace etc
// and call your catable function
php_catable($category_name);
?>
So that way your result will have been posted back to the server as per comments about client side/server side by @Fluinc and answer by @litelite . 所以,你的结果将被回发到服务器按有关客户端/服务器端评论这种方式@Fluinc ,回答@litelite 。 To get it to do something which performs looking like innerHTML which changes a part of the page without submitting the whole page you will need AJAX, again as per @litelite 's answer.
要使其执行类似于innerHTML的操作,该功能在不提交整个页面的情况下更改页面的一部分,您将需要AJAX,再次按照@litelite的回答。
Might get marked down for being dependant on JavaScript but intended mostly to help clarify client v server. 可能因依赖JavaScript而被贬低,但主要目的是帮助阐明客户端v服务器。
If you want to avoid the JavaScript dependency of this script you could leave out the onChange altogether and add a submit button, then collect $_POST['CategorySelect'];
如果要避免此脚本的JavaScript依赖性,可以完全省略onChange并添加一个提交按钮,然后收集
$_POST['CategorySelect'];
assuming that is its name - ensure it has name="CategorySelect"
for php as well as its Id for your css/javascript. 假设这是它的名称-确保它的
name="CategorySelect"
为php的name="CategorySelect"
,以及其CSS / javascript的ID。 Php gets its variable from the item's name. Php从项目名称获取其变量。
To get something a bit like the effect of AJAX visually (though the page is still submitted) you could submit the page to itself using action="<?php echo $_SERVER['PHP_SELF']; ?>"
on the form and have all the code on the one page. 要在视觉上获得类似于AJAX的效果(尽管仍在提交页面),您可以在表单上使用
action="<?php echo $_SERVER['PHP_SELF']; ?>"
将页面提交给自身,并具有一页上的所有代码。 You can put the table generating code in the div where you want the table to appear - it would need a default state set, of course. 您可以将表生成代码放在希望表出现的div中-当然,它需要一个默认状态集。
@litelite 's comment regarding not using posted data directly in an sql query is also vital to prevent attack - make sure you clean it up before you use it! @litelite关于不直接在sql查询中不使用发布的数据的评论对于防止攻击也很重要-确保在使用前清除它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.