2009-04-15 15 views
0

我现在有一个页面设置是这样的:如何控制PHP中的外观顺序?

PHP Variable Declarations 
HTML header, then a form to submit GET query 
The PHP that processes some stuff based on the GET data 
a bit of JavaScript that makes a pretty graph from the retrieved, processed data 

我想要做的是只具有形式出现第一次加载页面时,然后将其输入查询时,有图形出现在页面的底部。它使用的海军报库,和JS目前看起来是这样的:

<script id="source" language="javascript" type="text/javascript"> 
$.plot($("#test"), [ [[0, 0], [1, 1]] ], { yaxis: { max: 1 } }); 
</script> 

我是否需要更换$ .plot一些事件处理的东西吗?我是一位JS总新手!

谢谢!

+0

嗯,没有压力,但如果有一个这些答案适合你,你能接受吗?或者,如果失败了,让我们知道它是否行不通,我们可以帮忙吗?干杯! =) – 2009-04-16 19:26:07

回答

2

我个人使用PHP这个(但我在JS是垃圾......所以我有偏见.. 。=))

<?php 

$formSubmitted = $_POST['formSubmitted']; 

    if (!isset($formSubmitted)) { ?> 
    <form method="post" enctype="form/multipart" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

    <input name="formSubmitted" id="formSubmitted" type="hidden" value="1" /> 

    ... 

    </form> 
           <?php } 

    else { 

     // show the graph 

     } 

    ?> 
1

在你的文件,你会希望有类似以下内容:

<?php if ($someConditionalForTheForm) { ?> 
<form>....</form> 
<?php } else { ?> 
<script id="source" language="javascript" type="text/javascript"> 
$.plot($("#test"), [ [[0, 0], [1, 1]] ], { yaxis: { max: 1 } }); 
</script> 
<?php } ?> 
0
在你的PHP

,输出形式的代码,然后再看看是否形式已被提交:

if (isset($_GET['your_form_parameter'])) { 
    also_output_the_JS_graph; 
} 

这样的形式将始终显示,图表仅在提交表单时显示。

0

.js文件(jQuery的需要)

$(document).ready(function(){  
    $('#myformsubmit').click(function(){ 
     $.ajax({ 
      type: "GET", 
      url: "/your_data_processing_file.php", 
      data: "", 
      success: function(result){ 
      $("#graph").html(result); 
        /* 
        result is whatever you get from your script that handles 
        your submited data 
        */ 
      } 
     }); 
    }); 
}); 

的.html

<form id="myform">.....</form> 
    <div id="graph"><!-- here i want my result appear after submit 
without reloading the page, i'm so 1337 --></div> 

要更加冷静,你可以返回JSON数据结果,并通过你的图JS照顾那个。

+0

如果我正确地得到了你的问题,并且你对我的答案满意,我建议你将问题标题改为'如何通过js加载表单处理数据',问题不在于php的顺序码。 cheerz。 – zalew 2009-04-15 23:42:10