Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Passing js variable to php using ajax does not work

I want to get variable rating_idex in my php file so if is user click button #add-review it should pass in ajax variable and it will get array in php file and send review to the database, but it is not working and I don't see solution

$('#add-review').click(function(){
    var user_name = $('#reviewer-name').val();
    var user_review = $('#review').val();
    console.log(user_name);
    console.log(rating_index);
    console.log(user_review);
    if(user_name == '' || user_review == '')
    {
        alert("Please Fill Both Field");
        return false;
    }
    else
    {
        $.ajax({
            url:"rating-data.php",
            method:"GET",
            data:{
                rating_index: rating_index,
                user_name: user_name,
                user_review: user_review
            },
            success:function(data)
            {
                $('#review_modal').modal('hide');
                load_rating_data();
                console.log(data);
            }
        })
    }
});

This is my php code when I can get the variable and send them to the database:

<?php 
include 'connection.php';
echo ($rating_index);
if(isset($_GET["rating_index"]))
{
    $data = array(
        ':user_name'        =>  $_GET["user_name"],
        ':user_rating'      =>  $_GET["rating_index"],
        ':user_review'      =>  $_GET["user_review"],
        ':datetime'         =>  time()
    );
    $query = "
    INSERT INTO review_table 
    (user_name, user_rating, user_review, datetime) 
    VALUES (:user_name, :user_rating, :user_review, :datetime)
    ";

    $query_run = mysqli_query($conn, $query);
    if($query_run){
        echo "Your Review & Rating Successfully Submitted";
    } else{
        echo '<script type="text/javascript"> alert("Something went wrong") </script>';
        echo mysqli_error($conn);
    }
}
?>

When I am trying to echo ($rating_index) it give me feedback that variable does not exist so it is something with ajax but can't find solution, thanks in advance for any solutions

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Instead of echo ($rating_index); try echo ($_GET["rating_index"]); reason being you didn't actually declared $rating_index

about 4 years ago · Juan Pablo Isaza Denunciar

0

if I'm not wrong you want to pass the PHP variable in javascript? if yes you cant pass the PHP variable in js like this.

var x = " < ? php echo"$name" ? >";

you can pass your PHP variable like this but in only the .php file not in the .js

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda