简体   繁体   中英

How to open a specific modal after form submitted and location redirect

I got a form inside a modal, and when it's submitted i don't want it to close. They way i got my SQL UPDATE statement setup is so it will redirect to the same page if the datebase got updated, so this is messing alot of stuff up when trying to keep the modal open.

if (isset($_POST['insert6']))
{
    $kval_antall = $_POST['kval_antall'];
    $id = $_POST['id'];

    $sql6 = ("UPDATE test3 SET kval_antall='$kval_antall' WHERE id='$id'");


    if (mysqli_query($conn, $sql6)) {
    header("Location: aktivbonus.php");
    exit;
} else {
    echo "Error: " . $sql6 . "<br>" . mysqli_error($conn);
}}

Because if i put some code before and keep the modal open, if the data gets successfully updated in the database it will go back to the same page and close the modal anyway. I can't find a way to open the modal after i have redirected.

This is some of the stuff i have tried: (Answer from bishop) How to redirect back to index page and display modal pop up as per redirecting? PHP

The code works and displayes the message perfectly after the form has been submitted and redirected back, but when i insert this nothing happens:

<?php if (isset($_GET['thanks']) && 1 == $_GET['thanks']) { ?>
<script type='text/javascript'>
   $("#message539").modal("show");
</script>
<?php } ?>

The way i got my website working is that a button appears on the page with data-target="#message539" <- id changes, depening on which button you press.

Same with the modal its trying to call (Same here id changes):

<div class="modal fade bd-example-modal-lg" id="message'. $row['id'] .'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

In this change its the modal #message539, i'm trying to call. If someone got a good way of doing this, help is very much appreciated as i'm abit lost now with trying to many different things.

I tried my best explaning what i want and it can be hard to understad, so please ask if something is unclear. Thank you.

Modal

<?php
    while ($row = mysqli_fetch_assoc($result)) {
        $test55 = '<div class="card card-date"><div class="card-body card-body-date text-secondary"><h5>' . strftime('%e.%B',strtotime($row['date'])) . ' <div class="card-header-date"><i class="material-icons">arrow_downward</i></div></h5></div></div>';
        echo $test55 !== $prevDate ? $test55.'' : '';
        $prevDate = $test55;
        echo '
        <div class="card border-info card-margin">
            <h5 class="card-header text-secondary">
                '. $row['bookmaker']. ': '. $row['bettype']. ' '. $row['betvalue']. 'kr <div class="card-header-date">Dato lagt til: Kl.'. strftime('%H:%M, %e.%b',strtotime($row['date2'])) . '</div>
            </h5>
            <div class="card-body text-secondary">
                <h5 class="card-title">Status:</h5>
                <p class="card-text">' . $row ['status'] . '</p>
                </div>
            <div class="card-footer bg-transparent"><div class="text-right"><button type="button" class="btn btn-outline-info" data-toggle="modal" data-target="#message'.$row['id'].'">Endre</button></div></div>   <-- BUTTON THAT OPENS THE MODAL
            </div>
        </div>
        <div class="modal fade bd-example-modal-lg" id="message'. $row['id'] .'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <--- MODAL
          <div class="modal-dialog modal-lg" role="document">
              <div class="modal-content">
                <div class="modal-header card-header">
                  <h5 class="modal-title text-secondary" id="exampleModalLabel">'.$row['bookmaker'].': '.$row['bettype'].' '.$row['betvalue'].'kr</h5><div class="text-secondary" style="font-size: 1.25rem;font-weight: 500;">Dato lagt til: '.strftime('%H:%M, %e.%b',strtotime($row['date2'])).'</div>
                </div>
                <div class="modal-body">

You can use localStorage to keep track if you need to open the modal or not when you enter the redirect page:

When calling the update function:

localStorage.setItem('openModal', '#message539'); // Use message'. $row['id'] to dinamically save it

When loading the page:

 var modalId = localStorage.getItem('openModal');
 if (modalId != null){
  $(modalId).modal("show");
  localStorage.removeItem('openModal');
 }

use javascript or jQuery to do so: send a flag of success while redirecting, and check that flag on redirected page if its true than display modal using jQuery,

eg: (view page)

$flag = $flag?1:0;
<button id="btn-modal-open" style="display:none">clcik</button>

and in jQuery function

$( document ).ready(function() {
var flag = "<?= $flag?>";
if(flag){
$('#btn-modal-open')[0].click(); //create one
//button and give id as btn-modal-open so that on 
//click of button modal should popup
  }
});

need more explaination than tell me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM