Ajax not redirecting me to next page

后端 未结 2 1874
予麋鹿
予麋鹿 2021-01-28 22:04

I am trying to pass the ID of the clicked image to next page. When I developed my code, it didn\'t redirect me to the next page. When I click F12 and check the POST in network,

相关标签:
2条回答
  • 2021-01-28 22:44

    The entire point of Ajax is that that request is made with JavaScript and the response is handled by JavaScript without navigating to a new page.

    If you want to make a POST request and navigate to the resulting page, then use a <form>


    window.location.href = 'userevent.php';
    

    it redirect me but without the variable

    Assigning a URL to location.href triggers browser navigation (with a GET request) to that URL.

    It is a completely different request to the Ajax request so it doesn't have the data from that request.

    Again: Use a form for this.


    I read the data from database and I get the clicked id of the images.

    Put the data you want to send in a submit button instead.

    <form method="POST" action="userevent.php">
        <?php while($row = $result->fetch_assoc())  ?>
            <button name="clicked" value="<?php echo htmlspecialchars($row["ID"]); ?>">
                <img src="images/<?php echo htmlspecialchars($row["photo"]); ?> alt="Put useful and unique alt text so people know what they are selecting if they can't see the image">
            </button>
        <?php } ?>
     </form>
    
    0 讨论(0)
  • 2021-01-28 22:48

    I think you are better off making a small form, since you want to send the user to a new page when clicking.

    <?php while($row = $result->fetch_assoc())  ?>
        <form action="userevent.php" method="POST">
            <input type="hidden" name="clicked" value="$row["ID"]">
            <img src="images/{{ $row['photo'] }}" class="img">
        </form>
    <?php } ?>
    $('.img').click(function() {
        this.form.submit();
    });
    

    *Edited to reflect your current edits.

    0 讨论(0)
提交回复
热议问题