Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!