Two Actions Two Submit Button In Php? May 01, 2023 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> Copy Then let javascript control the flow of the submitting : var formElem = document.getElementById('formElem'), btnSubmit = document.getElementById('register'), btnPreview = document.getElementById('preview'); function formSubmit() { 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; Copy Solution 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(); ?> Copy The 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> Copy We process each of the buttons via their name in the POST array:Baca JugaDraw A Filled Polygon Using Scanline LoopSound Notifications In OperaCss Style Woocommerce Single Product Page <?php if(isset($_POST['submit'])){ foreach ($_POST as $key => $value) { $_SESSION[$key] = $value; } header("Location: form10.php"); } if(isset($_POST['preview'])){ foreach ($_POST as $key => $value) { $_SESSION[$key] = $value; } header("Location: form10_preview.php"); } ?> Copy And at the very end of the file, we flush the output buffer: <?php ob_end_flush(); ?> Copy So, 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 ($inputs as $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?"