• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

134
Views
Enviar una matriz de JavaScript a PHP a través de POST

Estoy tratando de enviar una matriz de JavaScript a un archivo PHP a través de POST.

JS:

 var songlist = ['song1', 'song2', 'song3']; var sendData = function(){ var data = songList.join(',') $.post('test.php', {data: data}).always(function() { window.location = 'test.php'; }); } sendData();

prueba.php:

 <?php $songData = $_POST['data']; $songData = explode(',', $songData); print_r(array_values($songData)); ?>

cuando enviarDatos(); me dirige a test.php obtengo:

Aviso: índice indefinido: datos

¿Por qué la variable de datos no tiene ningún valor cuando intento imprimirla o usarla?

about 3 years ago · Santiago Trujillo
2 answers
Answer question

0

Así no es como funciona la solicitud POST. Lea más sobre Ajax, pero por ahora, así es como debe hacerlo.

 var songlist = ['song1', 'song2', 'song3']; var sendData = function() { $.post('test.php', { data: songlist }, function(response) { console.log(response); }); } sendData();
 // test.php <?php $songData = $_POST['data']; print_r($songData); ?>

about 3 years ago · Santiago Trujillo Report

0

1) $.post('url') - La solicitud de Ajax se realiza mediante el método $.post() y ha proporcionado "testing.php" como URL que no es válida.

2) window.location = 'test.php' - Esto se usa para redirigir a una página específica y ha redirigido a 'test.php' sin ningún parámetro/dato. Es por eso que muestra "Aviso: índice indefinido: datos"

3) Trate de entender cómo funciona ajax. Siguelo -

 var songlist = ['song1', 'song2', 'song3']; var sendData = function() { $.post('test.php', { data: songlist }, function(response) { console.log(response); }); } sendData(); // test.php <?php if(isset($_POST)){ if(isset($_POST['data'])){ $songData = $_POST['data']; print_r($songData); }} ?>
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error