不知道如何解决(在PHP和Javascript之间) [英] Don't know how to solve (Between Php and Javascript)

查看:103
本文介绍了不知道如何解决(在PHP和Javascript之间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何通过以下脚本解决变量 $ cat text 从Form到javascript的变量,并传递给php函数为 $ Categories_name ,并成为 $ cat



我已经测试了 $ cat 变量,它不是String,它是对象,我不明白。



但是我需要 $ cat 为String。



When Test =这是猫(3字),

我用php str_word_count测试$ cat,输出为1(我需要更正答案3);



我测试$ cat by php var_dump并且没有输出(我需要更正答案String)。




 < p id =CaTable>< / p> 

< script>
函数CaFunction()
{
var text = document.getElementById(CategorySelect)。value;
document.getElementById(CaTable)。innerHTML =<?php php_catable('+ text +');?>;
}
< / script>

<! - 通过php和MySQL生成表格 - >
<?php
函数php_catable($ Categories_name)
{
$ cat = $ Categories_name;
.................
.................
$ sql = select * from table where xyz ='。$ cat。 ;
}
?>


解决方案

您的第一页:
假设 CategorySelect 是一个下拉选择框,为其onChange事件创建一个脚本,并创建一个 method =post隐藏的输入到generate_table.php。

 < input type =hiddenname =ca_tableid = ca_table/> 

使ca_table成为一个隐藏的输入,以便在提交此页时php会从中获取值

 < script language =javascripttype = text /的javascript> 
函数CaFunction(){
documentGetElementById('ca_table')。value = documentGetElementById('CategorySelect')。value;
submit();
}

< / script>

将其添加到您的选择下拉菜单中:

  onChange =CaFunction(); 

您的接收页面:
所以您的接收页面generate_table.php p>

 <?php 
函数php_catable($ Categories_name)
{
$ cat = $ Categories_name ;
.................
.................
$ sql = select * from table where xyz ='。$ cat。 ;
}


$ category_name = $ _POST ['ca_table']; //至少用合适的preg_replace等清理

//并调用你的可捕获函数
php_catable($ category_name);

?>

这样,您的结果将按照客户端/服务器的注释发回服务器通过 @Fluinc 并通过 @litelite 。为了让它做一些看起来像innerHTML的东西,它改变了页面的一部分而不提交整个页面,你将需要AJAX,同样是按照 @litelite 的答案。

可能因为依赖JavaScript而被标记,但主要是为了帮助澄清客户端v服务器。



如果您想要避免此脚本的JavaScript依赖性,您可以完全省略onChange并添加一个提交按钮,然后收集 $ _ POST ['CategorySelect']; 假设它是它的名字 - 确保它有 name =CategorySelect for php以及你的css / javascript的Id。 Php从项目名称中获取它的变量。



为了使AJAX具有可视化效果(尽管页面仍然提交),您可以将页面提交给自己在表单上使用 action =<?php echo $ _SERVER ['PHP_SELF'];?>,并将所有代码放在一页上。您可以将表格生成代码放在希望显示表格的div中 - 当然,它需要一个默认状态集合。



@litelite 对不直接使用发布数据的评论在一个sql查询中,对于防止攻击也很重要 - 确保在使用它之前清理它!


I don't know how to solve the variable $cat by following script.

"text" variable from Form to javascript and pass to php function to be $Categories_name and to be $cat.

I already test the $cat variable, it is not "String", it is "object", I don't understand.

But I need $cat to be "String".

when Test = "This is Cat" (3 words),

I test $cat by php str_word_count and the output is 1 (I need to correct answer 3);

I test $cat by php var_dump and no output (I need to correct answer "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. "'";
}
?>

解决方案

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".

 <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.

 <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

 <?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. 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.

Might get marked down for being dependant on JavaScript but intended mostly to help clarify client v server.

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']; assuming that is its name - ensure it has name="CategorySelect" for php as well as its Id for your css/javascript. Php gets its variable from the item's name.

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. 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.

@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!

这篇关于不知道如何解决(在PHP和Javascript之间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆