1

i have a form with several inputs and corresponding buttons. The input name attribute and button id are both the same. i.e

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

now what I want to do is get the id of the button click and then insert its value inside my php function code in the following places;

<?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();
}

?>

is this possible?

Omar999
  • 65
  • 1
  • 3
  • 8

2 Answers2

0

First of all, you have to decide what technologies are you going to use. To simplify this we can handle your problem only with PHP (you could use javascript with jQuery, even AJAX).

You only need one script. In this script (let's say it will be called update.php) there will be your form, condition which will catch your sent POST and inside this condition there will be code capable of handling your data and storing it correctly.

Your form needs to redirrect to the PHP script it is in. So you will need something like this:

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

For action attribute you can either use "#" or "update.php" - both works the same in this context. Then the function that will handle your form (put it into condition):

<?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']
    }
  }
?>

Please condider this a very easy tutorial. This code can definitely be optimalized and enhanced. Even though I believe you will find the use of it.

Dejv
  • 944
  • 2
  • 14
  • 31
0

Change your buttons to

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

Then in your PHP script, you check which button was clicked by doing:

<?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
}
?>
Jimmy Pelton
  • 11
  • 3
  • 16
  • thanks for the replies. is there any other way than if else statements as I will approximately have 25-30 different buttons. if/else 30 times doesnt seem efficient – Omar999 Sep 02 '13 at 20:56
  • 1
    You can add the possible buttons to an array, and then loop over them like this: `$buttons = array('titleButton', 'metaKeywordsButton', 'metaDescriptionButton'); foreach ($buttons as $button) { if (isset($_POST[$button])) { echo "$button was clicked"; } }` – Jimmy Pelton Sep 04 '13 at 02:13