Error 500 in WordPress Child Theme due to AJAX Internal Issue

I have encountered an issue with my Ajax code in the Js/Jq block (/buscador/menuLateral/menu-libros.php):

$.ajax({
  url: '<?= get_stylesheet_directory_uri(); ?>' +
    '/buscador/buscador-functions.php',
  type: 'POST',
  data: {
    sendCheckedVal: 'yes',
    tipo: 'editorial',
    valor: valor_recogido,
    archivo: this_file_id.toString()
  },
  success: function(data) {
    alert('Checked value !');
  }
});

The values are present in the file and I confirmed it by using a GET method with stringify.

To retrieve these values, a file like this is required (/buscador/buscador-functions.php):

<?php
    if (ISSET($_POST['sendCheckedVal'])){
       echo 'hi, u reached here' ;
    }
?>

Unfortunately, the values are not being passed from the js code file to the next step.

An error is showing up in the console:

POST [WP-URL-HIDDEN-ON-PURPOSE]/themes/ChildrenNewspaper/buscador/buscador-functions.php 500 (Internal Server Error)

The line causing the error is on the right side of :

jquery.min.js:2

If anyone knows how to fix this issue with Ajax in a WordPress theme, your help would be appreciated. Thank you.

Answer №1

The issue lies here:

alert('hi, you have reached this point');
. The alert() function is not a PHP function, but rather a JavaScript function. Instead of using it, you can utilize the echo or return functions.

<?php
    if (isset($_POST['sendCheckedVal'])){
       echo 'hi, you have reached this point';
    }
?>

Answer №2

Let's get started by understanding that the alert function is used in javascript. When working with php, you must use the echo statement to display values on the page;

If you prefer using JS, you will need to output js code.

echo "<script type='text/javascript'>alert('hi, u reached here');</script>";

Alternatively, you can also use echo to display variables on the page.

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

Obtaining encrypted data in json format

Received some JSON encoded data. How do I access each of the individual elements? Here's an example: $ticket $customer $user {"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"<a href="/cdn-cgi/l ...

Please consider opening in a new tab rather than a new window

<a href='#' onclick="loadpage();">[RANDOM PAGE]</a> Whenever this code is clicked, the following function gets executed. function loadpage(){ $.ajax ({ type: "POST", url: "fetchpage.php", data: "showpage=1", success: function(msg) { ...

Show a success message, clear the post data, and redirect back to the current page

I have a single web page with a contact form. When the form is submitted, I want it to redirect to the same page and display a success message that fades out after a few seconds. Additionally, I want the post data of the form to be cleared. The form also i ...

Switch the process to prioritize organization over functionality. Kindly aid me in this

I'm having trouble with my DB query. When I use the sortby company option, it only shows 18,000 records out of a total of 48,000 records. However, the sortby status, contact, and city options are working fine. Any assistance would be gr ...

What is the best way to implement a loading cursor when the Submit button is clicked?

Is there a way to incorporate a progress cursor into my code in order to notify the user to wait when they click the Submit button or the Upload Button while uploading multiple files? Below is an example of my form: <form action="" method="post" enct ...

Automating the movement of a slider input gradually throughout a specified duration

I am working on a website that includes a range input setup like this: <input type="range" min="1036000000000" max="1510462800000" value="0" class="slider" id ="slider"/> Additionally, I have integrated some D3 code for visualizations. You can view ...

Is it possible for the relative path of a gif image to become invalid when added to a div?

Question Context: In the view, I have a form that will show a loading 'spinner' gif when a user submits it. The Problem: If I place the spinner img element within its container div, the spinner is always displayed, which is not desired: https ...

Discovering precise terms in an array within a string using PHP

Looking for a way to find exact matches of words in an array within a string and inform users if any are found? Here is the code I have so far: <?php // Profanity check $profaneReport = ""; $allContent = "Rice Beans Class stite" ...

Data structure for Highcharts:

Recently, I've been experimenting with Highcharts (http://www.highcharts.com) in a test application built on rails 3.1.1 and HAML. As someone who is still new to JavaScript, I'm striving towards achieving a seamless integration of Highcharts. Wi ...

How to add multiple entries using Node.js and Tedious

I am currently working with an array of customer objects that I need to insert into a SQL database. These customer objects are retrieved from the request data. For this task, I am utilizing Tedious for handling the request and Tedious Connectionpool to ma ...

Sending replies through Twilio to a different phone number

I've been attempting to send a message or forward a message to another number through Twilio, but it doesn't seem to be working. Receiver: +12345678 Twilio Number: +987654321 header('Content-Type: text/html'); ?> <Response> ...

Searching patterns in Javascript code using regular expressions

I am dealing with two strings that contain image URLs: http://dfdkdlkdkldkldkldl.jpg (it is image src which starts with http and ends with an image) http://fflffllkfl Now I want to replace the "http://" with some text only on those URLs that are images. ...

React HTML ignore line break variable is a feature that allows developers to

Can you help me with adding a line break between two variables that will be displayed properly in my HTML output? I'm trying to create an object with a single description attribute using two text variables, and I need them to be separated by a line b ...

How come the link function of my directive isn't being triggered?

I'm encountering a strange issue with this directive: hpDsat.directive('ngElementReady', [function() { return { restrict: "A", link: function($scope, $element, $attributes) { // put watche ...

Navigating through a nested array within a JSON object using Vue.js - a guide

I have a JSON data array that includes outer and inner subarrays. My goal is to loop through both levels and create a table. Below you'll find a snippet of the sample array: { class:'I', subDdiv:[ { ...

The Node.js Express Server runs perfectly on my own machine, but encounters issues when deployed to another server

I've encountered a strange issue. My node.js server runs perfectly fine on my local machine, but when I SSH into a digital ocean server and try to run it there, I get this error. I used flightplan to transfer the files to the new machine. deploy@myse ...

Deleting entries from a selection of items in a list generated from an auto-fill textbox

I have successfully implemented an auto-complete Textbox along with a styled div underneath it. When the client selects an item from the Textbox, it appears in the div after applying CSS styling. Now, I am looking to create an event where clicking on the s ...

Stopping individuals from engaging in the act of spamming a picture upload form

I am currently developing an innovative Image hosting platform, and I am faced with a crucial challenge – how can I effectively prevent users from continuously refreshing the form, causing the image to be repeatedly uploaded until my disk space allocat ...

When using props.onChange(e.target.value) in a textField component in Material UI, it unexpectedly returns an object instead of a value

function FormInput(props) { const classes = formInputStyles(); return ( <div> <TextField onChange={(e) => props.onChange(e.target.value)} InputProps={{ classes, disableUnderline: true }} {...pro ...

Creating interactive forms with Symfony2 using embedded forms and event listeners

I created a form called "AddressType" with an event listener that functions correctly when used independently. Below is the code: namespace Nc\ClientsBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Componen ...