Error encountered when using the $.post function

$(".eventer button[name=unique]").click(function() { 
    console.log('button clicked'); 
    thisBtn = $(this); 
    parent = $(this).parent(); 
    num = parent.data('num'); 
    id = parent.data('id'); 

    if(typeof num != 'number'){ 
        num = 0; 
    } 

    $(this).attr('disabled', true); 
    $.post(
        'unique.php', 
        {
            num: (num+1), 
            id: id
        },
        function(data) { 
            console.log('Ajax success'); 

            parent.next('.status').html(num);  
            thisBtn.attr('disabled', false);
          
            console.log('Ajax success'); 
            parent.data('num', ++num); 
            parent.next('.status').html(num); 
            thisBtn.attr('disabled', false); // reset 
        }
    );
}); 

console.log('-- end'); 

An error occurs in unique.php where the index is undefined, indicating that 'num' is not being sent correctly to the page. Additionally, the expected HTML snippet from unique.php is not showing up on the .status element as intended.

This code belongs to unique.php. The 'num' variable should be posted here and then displayed by echoing the value in the correct status class on the main page. However, only the numerical value of 'num' appears on the main page instead.

<?php
   $uniqueVar = $_POST['num'];
   echo " $uniqueVar haha unique cakes";
?>

Answer №1

The issue preventing the PHP script snippet from being shown is due to the following line in the code:

parent.next('.status').html(num);

Instead of displaying the returned data, this line sets the status class to show the number you inputted.

To resolve this, update it to:

parent.next('.status').html(data);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using XSLT to convert HTML into the desired text format

It has been almost two decades since my last encounter with XSLT. I am attempting to transform documents, like the HTML snippet below, into the desired output. <p> おばあさんは、とても <ruby><rb>喜</rb><rp>(</rp ...

Utilize the sortable script for dynamically loaded items via AJAX

For the drag and drop feature on my website, I am using jQuery sortable. I have a button that displays results with items on the screen. These items can be dragged and dropped into various sections. The issue I'm facing is that if I include the sort ...

The customization of jQuery UI Slider is failing to function as expected

In my quest to create a customized jQuery slider, I encountered the need to change the background of the slider handle. After experimenting with some code on this link, I am still unable to achieve the desired effect. Following the documentation, I have a ...

Ways to split combined JSON data using jQuery?

I recently implemented a jQuery script that fetches data from a JSON server and stores it in an "output" variable. Here is how the code looks: $(function() { $.getJSON('http://127.0.0.1:8000/getData/', displayData); function displayData(da ...

Extract the content of a textbox within an iframe located in the parent window

Is it possible to retrieve the value of a text box in an iframe from the parent page upon clicking a button? Below is an example code snippet showcasing the situation: <div> <iframe src="test.html" > <input type=text id="parent_text"> & ...

Designing with Bootstrap 4 involves incorporating a button that uses the data-toggle attribute set to "collapse" along with an href anchor linked to

I have attempted to link to an ID while also triggering a data-toggle collapse: <a href="#id" data-toggle="collapse" data-target="#collapseTwo" class="btn btn-primary" title="Something cool">Something very cool</a> for linking to this specif ...

I find the SetInterval loop to be quite perplexing

HTML <div id="backspace" ng-click="deleteString(''); decrementCursor();"> JS <script> $scope.deleteString = function() { if($scope.cursorPosVal > 0){ //$scope.name = $scope.name - letter; ...

Tips for increasing the height of a popover when clicked

When a user focuses on the password input, a popover displays information. At the bottom of the popover, there is a link. How can I make the popover expand when the user clicks on this link? I have tried adding an !important class for the height value, us ...

Transmitting PHP object through AJAX

Can anyone suggest the most effective method for passing a PHP object via AJAX? For instance: //objectClass = objectClass.php $obj = new objectClass(); <a href="javascript:getOutput("some variable", $obj); Since the other file, output.php (called thr ...

Is the background image on my class website too large?

I am having an issue with the redbar.png image in my CSS. It is positioned too high (within #container) and overlapping with the horizontal nav bar when it shouldn't be. I'm uncertain how to fix this problem. Any suggestions would be greatly appr ...

Generate an array in JavaScript using the values from input fields

Is there a way to create a JavaScript array that stores the values of all input fields with the class 'agency_field', when there are x number of these fields in the form? I attempted to achieve this using jQuery, but encountered a syntax error. ...

Deleting a database query once the modal is closed

When visiting the artist's about page on my website, clicking on the "button" will open a modal that retrieves information from a database. Currently, when you click on one artist button, close the modal, and then click on another artist, both artists ...

How to Retrieve the Parent ID in jQuery Sortable?

I'm currently experimenting with the sortable feature and encountering a minor roadblock. My aim is to extract the ID of the new parent element for use in an ajax request. UPDATE: I've managed to log the parent element, but it's duplicatin ...

Prevent clicking until the IE 10 and 9 windows have fully loaded

My goal is to prevent clicking on an image element until all the resources have finished loading, enabling the click only after the window.load() or when document.readystate reaches complete status. While this code works well in Chrome and Safari, I am fac ...

Using a combination of PHP and JQuery, I noticed that adding an IF statement within the document ready function

In the (document).ready function, the code below is used to properly display the order form: <?php if (isset($_POST['preview-order'])) { //IF PREVIEW FORM ?> $("#cam-order-preview").show('slow'); <?ph ...

Refreshing div content based on dropdown selection without reloading the page

I am currently working on implementing a dynamic dropdown feature that will update text content on a webpage without needing to refresh the entire page. The updated text will be fetched from a PHP function which receives input from the dropdown selection. ...

HTML - Selecting Different Values in One Drop Down Based on Another Drop Down

When selecting "First Year" in the initial drop-down menu, the options "Sem1" and "Sem2" should be displayed in the second drop-down menu. Similarly, when choosing "Second Year" in the first drop-down menu, the choices "Sem3" and "Sem4" should appear in th ...

Steps for assigning a default attribute to all elements

Let's take a look at this code snippet: <form> <input type="text" required="true"> <input type="text" required="true"> <button type="submit">Click</button> </form> Do you think there is a way to assign a def ...

Combining two arrays in JavaScript and saving the result as an XLS file

I have a question that I couldn't find an answer to. I need to merge two arrays and export them to an Excel file using only JavaScript/jQuery. Let's say we have two arrays: Array 1 : ["Item 1", "Item 2"] Array 2 : ["Item 3", "Item 4"] When the ...

Unique CSS content for varied designs

I have a CSS code snippet for a tooltip: .GeneralTooltip { background:#c7430f; margin:0 auto; text-transform:uppercase; font-family:Arial, sans-serif; font-size:18px; vertical-align: middle; position:relative; } .GeneralTooltip::before { content:"This is ...