-2

i need to transfer a variable from js to a php page then insert it in the database

the Js code

    function tableText(tableCell) {
    var x =tableCell.innerHTML;
     console.log(x);
}

also tried the ajax

function tableText(tableCell) {
    var x =tableCell.innerHTML;
    console.log(x);
    $.ajax({  
    type: 'POST',
    url: 'try.php', 
    data: x ,
    complete: function(text){
         return text; }
 });
}

not sure if i got it right and i don't know how to call it in the php page

so How to get variable from JavaScript to PHP ??

3
  • read more about jquery's ajax function - the data you send should be an object, not a simple value
    – Kinglish
    May 5, 2022 at 19:53
  • You probably want to send the entire html table and save it in a database table, you can learn these basic (insert/select/update/delete) or CRUD operations easily . Juste get your self a cup of coffee , a good YouTube tutorial , couple hours of free time and some enthusiasm towards programming. May 5, 2022 at 19:58
  • ohh no no no i know how to (insert/select/update/delete) prefectly i just need to transfer one Js variable to another php page .... May 5, 2022 at 20:01

2 Answers 2

2

Try this one: (Edit, explanation set json)

function tableText(tableCell) {
    var x =tableCell.innerHTML;
    console.log(x);
    $.ajax({  
    type: 'POST',
    url: 'try.php', 
    dataType: "json",
        data: { x:x },
    complete: function(text){
         return text; }
 });
}

function tableText(tableCell) {
    var x =tableCell.innerHTML;
    console.log(x);
    $.ajax({  
    type: 'POST',
    url: 'try.php', 
    dataType: "json",
        data: { x:x },
    success : function(text){
            $('#MyDiv').html(text);
        }
 });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You can retrieve by $_POST['x'] just like a normal post.

For the return in php you need to use echo json_encode(['text'=>'ok']);.

7
  • @Kid thank you i thinkmit worked but how do i echo the variable in question the x ? sorry i'm just starting php and js May 5, 2022 at 20:54
  • 1
    Please explain your code, it isn't useful when you just post Try this: [code]
    – ethry
    May 5, 2022 at 20:54
  • @OumniaRanaSEGUENI you can find it in the $_POST variable in PHP. It would be $_POST['x'] I believe.
    – ethry
    May 5, 2022 at 20:55
  • way too better answer than the other, HOWEVER jQuery is not necessary needed, you can use XMLHttpRequest or Fetch API to do this even better than jQuery (avoiding callback hell) May 5, 2022 at 21:10
  • as @ethry explained correctly, echo $_POST['x']; will absolutely work, but try saving it in a local variable instead of the SUPER GLOBAL $_POST and before using it's value I suggest you to see if it has been posted correctly to your page using if ( isset( $_POST['x] ) ) { /* Your Other Codes Based On x*/} ,, and you can mark the answer as accepted if it worked and solved your problem May 5, 2022 at 21:13
0

You are not sending your data correctly with ajax, your ajax should look like this

Edit: Add a sweet alert to display a nice looking message after your insert

function tableText(tableCell) {
    var x =tableCell.innerHTML;
    console.log(x);
    $.ajax({  
        type: 'POST',
        dataType: 'json',
        url: 'try.php', 
        data: {
            x:x
        },
        success: function(text) {
            //SWAL
            Swal.fire(
                'Success!!',
                text,
                'success'
            )
        }
     });
 }


<!-- SWEET ALERT -->
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

and on your try.php file

<?php
$_REQUEST['x'];//here you receive what you send thru ajax

//here goes your insert

echo json_encode('insert was successfull')//this msg is what you will see when you return text
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.