get id of button once clicked using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omar999
    New Member
    • Mar 2010
    • 120

    get id of button once clicked using php

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

    Code:
    	<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;

    Code:
    	<?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('/\$**[B]INSERT_BUTTON_ID_HERE[/B]**=\"(.*?)\";/', '$**[B]INSERT_BUTTON_ID_HERE[/B]**="'.$_POST["**[B]INSERT_BUTTON_ID_HERE[/B]**"].'";', $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['**[B]INSERT_BUTTON_ID_HERE[/B]**'])) {
    		update();
    	}
    
    	?>
    is this possible?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    What about saving your form to i.e. testform.php like this:
    Code:
    <?php
    var_dump($_POST);
    ?>
    <form method="post" action="testform.php">
        <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>
    and see what happens when you click..... ;)

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      I recommend to use a form for each button/text field, so you can get only the name of the clicked pair.

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        This would be easier:
        Code:
        <?php
        if (isset($_POST["title"])) {
           var_dump($_POST);
        } else {
           echo "'title' not set....";
        }
        ?>
        <form method="post" action="testform.php">
            <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>
        if you then visit http:....../testform.php?bl a=bla
        you wil get the message 'title' not set.

        Of course the check should be expanded...... ;)

        Comment

        Working...