2012-05-19 187 views
0

请参阅下面的代码。目标如下。当用户按下按钮时,应执行功能main.php。一旦完成,DIV opt_container的内容应通过运行功能ganttchart.php填写。确实我不知道如何:填充DIV容器

  1. 执行ganttchart.php之后main.php之后。从form运行main.php

由于我是PHP新手,任何建议都会对我很有帮助。

<div id="fragment-2"> 
    <table width="100%"> 
     <tr> 
      <td width="100%"> 
       <form name="optform" method="post" action=""> 
        <div class="buttons"> 
         <a href="" class="regular" onclick="click_function();return false;"> 
         <img src="images/opt.png" alt=""/> Run optimizaton 
         </a> 
        </div> 
       </form> 
      </td> 
     </tr> 
    </table> 
</div> 

<div id="opt_container"> 

</div> 

<script language="javascript"> 
function click_function() { 
    $('#opt_container').load('optim/mainOptim.php'); 
} 
</script> 

回答

2

你想做的事情不能在PHP中完成。 PHP脚本在远程服务器上执行,这意味着需要调用一个请求以便PHP返回一些内容。用户在他的本地(客户端)机器上进行操作。

onclick是执行javascript代码的处理程序,而不是php。您需要创建javascript函数,对您想要执行的php代码执行ajax请求,然后将响应放在html文档中。但是回应应该已经由ganttchart.php做了什么。

+0

请问您可以发布一些网页链接的例子吗? –

+0

http://www.howtoplaza.com/how-to-make-ajax-calls-with-jquery http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make- ajax-calls-with-jquery/ 并查看google的其他结果:https://www.google.pl/search?sourceid=chrome&client=ubuntu&channel=cs&ie=UTF-8&q=how+to+make+ajax+call + with + jquery – Zefiryn

+0

请查看更新的主题。我期待'main.php'的内容在DIV opt_container中显示,尽管它不能以这种方式工作。什么可能是错的? –

1

您不向<form>发布任何数据到服务器端。将这些GWT参数设为您的<img src=""/>会更简单。现在无需发布表单。

例如:

<div class="scrollbar" id="chart"> 
    <img src="ganttchart.php?param1=foo&param2=bar"> 
</div> 

否则,你需要以下流程。 Google上可以找到各个步骤的大量示例。

  1. 加载主机网页。
  2. 让用户XHR获得run.php所需的参数,并将你的calc计算到你的服务器(在主机页面使用一些JS)。
  3. 将''设置为所需图像的所需ganttchart.php请求。您可能需要一个'id'来标识(2)中的交易。即ganttchart.php?id = 123(在主机页面使用一些JS完成)
+0

难道我理解正确的话,你认为“参数1 =富&参数2 =栏”可能是我的职责“main.php”的输出?如果这是正确的,那么这不是我所需要的。 'main.php'将一些数据存储在数据库中,'ganttchart.php'使用DB数据创建图表。 –

+0

参数应该是函数的输入。为什么不结合run.php和ganttchart.php?不需要额外的电话或数据库和表单帖子来回传递数据。如果您必须使用数据库执行此操作,请使用AJAX请求发送数据,而不是使用表单POST。 –

1

首先,如果你想让PHP运行某些你可以访问它的文件,将它包含在你的文件中,或通过XHR请求它。

就你而言,你希望你的main.php文件在用户按下按钮时执行。这就是说,你可以使用ff。代码作为基础:

<!-- 
note: This code is just an example, 
     your code may vary to according 
     to your requirements. 
--> 

<form action="" method="get"> 

    <input type="hidden" name="run_main_function" value="yes"/> 

    <input type="submit" /> 

</form> 

<?php 

if(isset($_GET['run_main_function'])): 

    require_once 'path/to/your/main.php'; 

    /* execute something from your main.php */ 

    require_once 'path/to/your/ganttchart.php'; 

    /* execute some functions in your ganttchart.php */ 

    $result = function_from_ganttchart(); 

?> 

<div id="opt_container"> 

     <table width="100%"> 

      <tr> 

       <td width="100%"> 

        <div class="scrollbar" id="chart"> 

         <img src="<?php echo $result; ?>"> 

        </div> 

       </td> 

      </tr> 

</div 
</php 
    endif; 
?> 

或者您可以通过XHR向JavaScript请求您的甘特图的内容。

谢谢:)

+0

我试过你的例子。查看更新的主题。不知道为什么,但是当我点击按钮时,它会将我重定向到index.php页面... –

+0

我的问题是,您的ganttchart.php如何生成图像文件?通过$ _GET请求变量或$ _POST等?我不确定。谢谢 –

+0

[不知道为什么,但是当我点击按钮,它将我重定向到index.php页面] - 奇怪,除非你通过index.php作为控制器加载不同的模块。 –