0

I'm not able to make PHP read a text sent through Ajax.

index.html

<head>
    <!--CSS Bootstrap-->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin=" anonymous">

    <!-- JS -->
    <script src="https://code.jquery.com/jquery-3.6.0.js"
            integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>

<!-- Button to trigger modal -->
<button type="button" class="btn btn-primary" id="btnOpen">
    Open demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="modalExample" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
     aria-hidden="true">
    <?php include "modal.php"; ?>
</div>

<script>
    // call the image modal
    $("#btnOpen").click(function () {

       //send the string to the php page
        $.ajax({
            type: 'POST',
            url: '//localhost/slider/modal.php',
            data: 'Test shipping',
        }).done(function () {
            alert('data sent');
        });

        // call the modal window
        $("#modalExample").modal("show");
    });

</script>

modal.php

<div style="text-align: center">
    <?php echo $_POST['data']; ?>
</div>
<div id="carouselExampleIndicators" class="carousel slide" 
     data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" 
            class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="d-block w-100" src="img.png" alt="First slide">
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="img2.png" alt="Second slide">
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" src="img3.png" alt="Third slide">
        </div>
    </div>
    <a class="carousel-control-prev" href="#carouselExampleIndicators" 
       role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselExampleIndicators" 
       role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

When I click on the button, I get the message data sent from Ajax, the slider is displayed without errors, however, the text in the div where PHP should read the Post shows the error:

Notice: Undefined index: data in...

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • Thanks Ken but the error Undefined Index remains – Valdir Sola Apr 28 '22 at 00:49
  • The error message doesn't appear but neither does the text in the Div. – Valdir Sola Apr 28 '22 at 00:56
  • If you're expecting `data` as a key then your POST code should look like: `data: { data: 'Test shipping' },` . For clarity try changing it to `data: { msg : 'Test shipping' },` and use `POST['msg']`; The `data` is the jquery parameter name. – Computable Apr 28 '22 at 01:08
  • Thanks B__ I made the changes but same error message – Valdir Sola Apr 28 '22 at 01:19
  • Seems odd you are both `include`ing `modal.php` and also invoking it using ajax. The included version would not have a `POST` value. – Computable Apr 28 '22 at 01:27
  • I also tried to do this on the same page, without using the php include and the result was the same. – Valdir Sola Apr 28 '22 at 01:29
  • Seems like you are not actually doing anything with the ajax response (like updating a div) - for test try changing your `done` callback to : `done(function(data) { alert(data); });` - you should see the full php generation and then you must figure out what to do with it. Ajax is not the same as reloading a page. (or just console.log(data)). – Computable Apr 28 '22 at 01:39
  • Got it, I'll try and let you know the result. – Valdir Sola Apr 28 '22 at 01:46
  • Altert's return displays:
    test shipping
    and the rest of the html that is in modal.php I still don't understand why you didn't update the page
    – Valdir Sola Apr 28 '22 at 02:04

1 Answers1

0

You need to send the data as a object. Like this:

$.ajax({
    type: 'POST',
    url: '//localhost/slider/modal.php',
    data: {data: 'Test shipping'},
}).done(function() {
    alert('data sent');
});
Gustavo
  • 9
  • 1
  • 2
  • You can always use this for see what's inside the post data. `var_dump($_POST);` – Gustavo Apr 28 '22 at 01:10
  • Thanks Gustavo but same error message. var_dump does not show anything – Valdir Sola Apr 28 '22 at 01:19
  • The advice in this answer can be found in countless other pages on Stack Overflow. Please only provide unique answers. If a page is Resolved Elsewhere, please flag or vote to close as a duplicate. If you don't yet have that privilege, leave a comment. If you cannot do any of those things, please ignore the page and find good, unique, on-topic questions to answer. After answering a few of these well, you will have enough rep to unlock the aforementioned privileges. – mickmackusa Apr 28 '22 at 03:54