从两个不同的按钮调用php内容

时间:2014-04-08 07:12:35

标签: javascript php html

我有一个php文件,我在文件中创建了2个按钮。

我想点击获取

时访问Get.php文件
<?php
 $file = "xxx.json";
 include 'Get.php';
 ob_start();
 formatPara("0");
 $output = ob_get_clean();
 file_put_contents($file,$output);
 ?>
选择设置按钮时,

和Set.php文件。

 <?php
 $file = "xxx.json";
 include 'Set.php';
 ob_start();
 formatPara("0");
 $output = ob_get_clean();
 file_put_contents($file,$output);
 ?>

我的文件visual.php文件内容是:

<!DOCTYPE html>
 <meta charset="utf-8">

 <?php
 $file = "xxx.json";
 include 'Get.php';
 ob_start();
 formatPara("0");
 $output = ob_get_clean();
 file_put_contents($file,$output);
 ?>

 <style>
right { 
display:table-cell; 
vertical-align:top; 
width:300px; 
padding:0 5px;
 }

<?php 
$color=include('Color.php');
echo $color;
?>   
</style>

    <body>
<div id=tt>
     <button type="submit" onclick="get()"> <b>get</b></button>
     <button type="submit" onclick="set()"> <b>set</b></button>
</div>
     <div id=graph>
<script src="ll.js"></script>
<script src="visual.js"></script>

<script type="text/javascript">


window.onload = function(){
}
<?php
$link=include('Link.php');
echo $link;
?>
</script>
</div>
    <body>

我不知道如何获取Get.php文件和Set.php文件的内容。我知道我可以拨打ajax电话。但在这种情况下,我只能获取Get.php或Set.php文件的内容。 但是,我怎样才能使下面显示的其他数据按顺序执行。

<?php
 $file = "xxx.json";
 include 'Get.php';
 ob_start();
 formatPara("0");
 $output = ob_get_clean();
 file_put_contents($file,$output);
 ?>

我是php的新手。谢谢你的帮助。

3 个答案:

答案 0 :(得分:0)

顺便说一句,使用标准的html方法:表单元素的名称值

<body>
<form method="GET">
  <button type="submit" name="event" value="get"> <b>get</b></button>
  <button type="submit" name="event" value="set"> <b>set</b></button>
</form>
<body>

和php代码:

$file = "xxx.json";

switch ($_GET['event'])
{
    case 'get': include 'Get.php'; break;
    case 'set': include 'Set.php'; break;
}

ob_start();
formatPara("0");
$output = ob_get_clean();
file_put_contents($file,$output);

答案 1 :(得分:0)

Dmitriy的答案很简单,看起来很整洁。我喜欢。 我已经做了一个更长的例子,利用一个班级,并在整个评论它,或许你可以学习一两个。希望它有所帮助!

<?php
    class HandleFile {

        protected $file;

        //The construct is called, when the object is created. We request the file here.
        public function __construct($file = null) {
            //Lets check if the file variable is set or not.
            if( !is_null($file) ) {
                //Lets check if the file actually exists
                if( file_exists($file) ) {
                    //If the file exists, set the class variable.
                    $this->file = $file;
                } else {
                    //If the file does not exist. Throw an exception
                    throw new Exception("Construct:: The file " . $file . ", does not exist.");
                }
            } else {
                    //If the file variable is not set, throw an exception.
                    throw new Exception("Construct:: No file specified.");
            }
        }

        //This function will do our actual logic.
        public function doAction($fileToInclude) {
            //First, check if the file to include exists.
            if( file_exists($fileToInclude) ) {
                //Include it
                include($fileToInclude);
                ob_start();
                formatPara("0");
                $output = ob_get_clean();
                file_put_contents($this->file,$output);
            } else {
                //If it does not exist. Throw exception.
                throw new Exception("doAction:: File " . $fileToInclude . ", does not exist.");
            }
        }
    }

    //Try/catch. Tries the action and if an error is trown, catch it and output the message.
    try {
        $Handler = new HandleFile("xxx.json");
    } catch(Exception $e) {
        print $e->getMessage() . "<br>";
    }
    //Define a helper variable
    $action = null;

    //If the GET parameter "get" is set, do a get action
    if( isset( $_GET['get'] ) ) {
        $action = "Get.php";

    //If the GET parameter "post" is set, do a post action.
    } elseif( isset( $_GET['post'] ) ) {
        $action = "Post.php";
    }

    //If our helper variable has changed, there must be an action to do.
    if( !is_null($action) ) {
         //Try/catch. Tries the action and if an error is trown, catch it and output the message.
        try {
            $Handler->doAction($action);
        } catch(Exception $e) {
            print $e->getMessage() . "<br>";
        }
    }
?>

<body>
 <form action="" method="GET">
     <button type="submit" name="get" value="yes"> <b>get</b></button>
     <button type="submit" name="post" value="yes"> <b>set</b></button>
 </form>
<body>

答案 2 :(得分:-1)

您正在调用一个未设置的功能......

为什么不使用$ _POST 以2种不同的形式

1个隐藏输入包含id类型

如果类型为get,则包含get

如果设置了类型,则包含集合

这只是一个例子&#39;