使用php点击后获取按钮的ID

时间:2013-09-02 17:10:38

标签: php

我有一个带有几个输入和相应按钮的表单。输入名称属性和按钮ID都相同。即

<form method="post" action="update.html">
    <input type="text" name="title">
    <button id="title">Submit</button>

    <input type="text" name="metaKeywords">
    <button id="metaKeywords">Submit</button>

    <input type="text" name="metaDescription">
    <button id="metaDescription">Submit</button>
</form>

现在我要做的是获取按钮的id,然后在我的php函数代码中将其值插入以下位置;

<?php

function update() {

    // specify target file name to update   
    $fileName = 'variables.php';

    // Let's make sure the file exists and is writable first.
    if (is_writable($fileName)) {

        // load target filename contents inside a variable
        $content = file_get_contents($fileName);

        // use reg ex to find and replace variable contents within target filename
        $content = preg_replace('/\$**INSERT_BUTTON_ID_HERE**=\"(.*?)\";/', '$**INSERT_BUTTON_ID_HERE**="'.$_POST["**INSERT_BUTTON_ID_HERE**"].'";', $content);

        // open target filename for writing
        $handle = fopen($fileName, 'w');

        // Write $content to our opened file.
        fwrite($handle, $content);

        // success message
        echo "<p>Success, localisation file updated.</p>";

        // close opened file
        fclose($handle);

    } else {

        echo "<p class='errorMessage'>The localisation file is not writable</p>";

    }

}

if (!empty($_POST['**INSERT_BUTTON_ID_HERE**'])) {
    update();
}

?>

这可能吗?

2 个答案:

答案 0 :(得分:0)

首先,您必须决定要使用哪些技术。为了简化这一点,我们只能使用PHP处理您的问题(您可以使用jQuery,甚至是AJAX)。

您只需要一个脚本。在这个脚本中(假设它将被称为update.php),将有你的表单,条件将捕获你发送的POST,在这种情况下,将有代码能够处理你的数据并正确存储它。

您的表单需要重新连接到它所在的 PHP脚本。所以您需要这样的内容:

<form method="post" action="#">
    <input type="submit" name="submit1" value="Submit 1" />
    <input type="submit" name="submit2" value="Submit 2" />
    <input type="submit" name="submit1" value="Submit 3" />

    <input type="hidden" name="submit1-data" value="title" />
    <input type="hidden" name="submit2-data" value="metaKeywords" />
    <input type="hidden" name="submit2-data" value="metaDescription" />
</form>

对于action属性,您可以使用“#”或“update.php” - 两者在此上下文中的工作方式相同。 然后是处理表单的函数(将其置于条件中):

<?php
  if($_POST){
    if(isset($_POST['submit1'])){
      //do whatever with variable $_POST['submit1-data']
    }elseif(isset($_POST['submit2']){
      //do whatever with variable $_POST['submit2-data']
    }elseif(isset($_POST['submit3']){
      //do whatever with variable $_POST['submit3-data']
    }
  }
?>

请指出这是一个非常简单的教程。这段代码绝对可以优化和增强。即使我相信你会发现它的使用。

答案 1 :(得分:0)

将按钮更改为

<form method="post" action="update.html">
  <input type="text" name="title">
  <input type='submit' name="titleButton">

  <input type="text" name="metaKeywords">
  <input type='submit' name="metaKeywordsButton">

  <input type="text" name="metaDescription">
  <input type='submit' name="metaDescriptionButton">
</form>

然后在PHP脚本中,通过执行以下操作检查单击了哪个按钮:

<?php
if (isset($_POST['titleButton'])) {
  //Clicked button was title button
} elseif (isset($_POST['metaKeywordsButton'])) {
  //Clicked button was metaKeywordsButton
} elseif (isset($_POST['metaDescriptionButton'])) {
  //Clicked button was metaDescriptionButton
}
?>