Two Actions Two Submit Button In Php? February 28, 2024 Post a Comment I've form tag like this sample name:register.php page Solution 1: I think it is better to control form submit rules clientside. Remove the action from your form, and change the button type to be button :<form id="formElem" name="formElem" action="" method="post"> <input id="pd" name="pd"type="text" AUTOCOMPLETE=OFF /> <input id="pd1" name="fname"type="text" AUTOCOMPLETE=OFF /> <input id="pd2" name="mname"type="text" AUTOCOMPLETE=OFF /> <input id="pd2" name="lname"type="text" AUTOCOMPLETE=OFF /> 6 more input boxes <button id="register"type="button">Register</button> <button id="preview"type="button">Preview</button> </form> CopyThen let javascript control the flow of the submitting :var formElem = document.getElementById('formElem'), btnSubmit = document.getElementById('register'), btnPreview = document.getElementById('preview'); functionformSubmit() { switch (this.id) { case'register' : formElem.action='post10.php'; break; case'preview' : formElem.action='preview10.php'; break; } formElem.submit(); } btnSubmit.onclick = formSubmit; btnPreview.onclick = formSubmit; CopySolution 2: You could have the form point to its own page and handle each submit value separately. At the top of the file with the form, you'll need to start the output buffer and a session. This allows the use of header() to redirect, and storage of session variables.<?php ob_start(); session_start(); ?>CopyThe form will point to itself by removing the action attribute:<form id="formElem" name="formElem" method="post"> <input id="pd" name="pd"type="text" AUTOCOMPLETE=OFF /> <input id="pd1" name="fname"type="text" AUTOCOMPLETE=OFF /> <input id="pd2" name="mname"type="text" AUTOCOMPLETE=OFF /> <input id="pd2" name="lname"type="text" AUTOCOMPLETE=OFF /> 6 more input boxes <button name="submit"type="submit">Register</button> <button name="preview"type="submit">Preview</button> </form> CopyWe process each of the buttons via their name in the POST array:<?phpif(isset($_POST['submit'])){ foreach ($_POSTas$key => $value) { $_SESSION[$key] = $value; } header("Location: form10.php"); } if(isset($_POST['preview'])){ foreach ($_POSTas$key => $value) { $_SESSION[$key] = $value; } header("Location: form10_preview.php"); } ?>CopyAnd at the very end of the file, we flush the output buffer:<?php ob_end_flush(); ?>CopySo, essentially the form has one action, which is to submit the values to itself. Finally, both form10.php and form10_preview.php will need session_start(); at the top of the file to access the Session variables we've created, like so:<?php session_start(); $inputs = array("pd", "fname", "mname", "lname", etc...); foreach ($inputsas$input) { echo$_SESSION[$input]; } ?>Copy Share Post a Comment for "Two Actions Two Submit Button In Php?"
Post a Comment for "Two Actions Two Submit Button In Php?"