最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open a specific modal after form submitted and location redirect - Stack Overflow

programmeradmin4浏览0评论

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">

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">
Share Improve this question edited Feb 21, 2019 at 12:34 Seb asked Feb 21, 2019 at 12:18 SebSeb 5251 gold badge5 silver badges27 bronze badges 2
  • Please post the code of your modal and how you open it. You need to reopen the modal, when the you redirect to the page. You could call the page with a flag, to reopen the modal. E.g. you could redirect to the page using header("Location: aktivbonus.php?reopenmodal=true"); And if this flag is set, you attach the (javascript) code to reopen the modal. – Marvin Klar Commented Feb 21, 2019 at 12:29
  • @MarvinKlar Will post the code. I have already tried that, but it wont work and i think it is because it can't find the id of the modal before the button is pressed to open it. (Because its getting the id and creating the modal id when the button is pressed) – Seb Commented Feb 21, 2019 at 12:31
Add a ment  | 

2 Answers 2

Reset to default 6

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

发布评论

评论列表(0)

  1. 暂无评论