guys i need help, how could i get the id of a button? i have this code

echo "form action = 'sec_ph_fileMain.php' method = 'post'>";
echo "<input type = 'submit' name = 'upload' value = 'Upload' id = 'hi'>";

//variable declaration
$upload = $_POST['upload'];

what i want is that, when the user clicks the upload button, it will display the id of that button.

Dani AI

Generated

Brief recap: the goal was to know which button was clicked so you can read its id on submit. As suggested, a simple no-JavaScript fix is to put the identifier into a submitted field (hidden input) before the form is sent — that will arrive in $_POST and is fine for most cases, which is why was able to confirm it worked.

If you prefer server-side detection without an extra hidden field, give each submit a name/value and read that on the server. Example HTML and PHP pattern:

<button type="submit" name="action" value="upload" id="btnUpload">Upload</button>
if (!empty($_POST['action'])) {
  $clicked = $_POST['action'];           // "upload"
  echo 'Clicked: ' . htmlspecialchars($clicked, ENT_QUOTES, 'UTF-8');
}

If you want client-side detection (or need the actual DOM id), modern browsers expose the element that triggered submit via SubmitEvent.submitter. Use it in a submit handler and fall back for older browsers:

form.addEventListener('submit', function(e) {
  const btn = e.submitter || document.activeElement;
  const id = btn && btn.id ? btn.id : null;
  console.log('button id:', id);
  // optionally place id into a hidden field or send via fetch
});

See SubmitEvent.submitter (MDN) for browser details.

Quick troubleshooting: make sure the control you expect to send something has a name (only named submit controls are included in form data when they trigger the submit), ensure id values are unique, and sanitize any posted values before echoing or using them. If using PHP echo to emit HTML, use consistent quoting (or heredoc) to avoid broken tags.

Recommended Answers

All 2 Replies

Add a new input tag

<input type="hidden" name="id" value="hi">

thanks sir, i already got the code..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.