Elara Posted October 12, 2021 Share Posted October 12, 2021 I have here a select query where in I have to get the $den_name and $currDate from a js variable. $disData="SELECT bDate.* FROM (SELECT bDentist.* FROM ( SELECT bBranch.*,concat(px_fname,' ', px_lname) as px_name, concat(px_contact,' / ', px_contact2) as px_connum FROM appointments bBranch WHERE bBranch.brnch_id = '$id') AS bDentist WHERE bDentist.den_name = '$dentistName') AS bDate WHERE bDate.s_date = '$currDate'"; The reason for doing this is so that I can output the records when I clicked a specific date and dentist #1 that matches their values. I tried using ajax here is my code for trying to get the dentist's name // JS Var currDen to PHP Var const den1 = document.querySelector('.D1'); const den2 = document.querySelector('.D2'); const den3 = document.querySelector('.D3'); let currDen = ''; // if 'active' exists if( den1.classList.contains('active')) { currDen = document.getElementById("D1").innerHTML; }else if (den2.classList.contains('active')){ currDen = document.getElementById("D2").innerHTML; }else if (den3.classList.contains('active')){ currDen = document.getElementById("D3").innerHTML; }else{ currDen = '땡!'; }; console.log(currDen); const xhrDen = new XMLHttpRequest(); jsonStr = JSON.stringify(currDen); console.log (jsonStr); xhrDen.open("POST" , "clickedD.php"); xhrDen.setRequestHeader("Content-type" , "application/json"); xhrDen.send(jsonStr); }); }); this is the clickedD.php where I post it <?php $requestPayload= file_get_contents("php://input"); var_dump($requestPayload); $dentistName = json_decode($requestPayload,true); var_dump($dentistName); echo $dentistName; and in does return this to the network dev tool I also tried checking it if its empty with this if (empty($requestPayload)) { echo "Variable is empty.<br>"; } else { echo $requestPayload; } if (empty($dentistName)) { echo "Variable is empty.<br>"; } and this is what it output what is wrong with my code ? please help me out Quote Link to comment https://forums.phpfreaks.com/topic/313963-how-can-i-use-a-js-variable-as-a-php-variable/ Share on other sites More sharing options...
requinix Posted October 12, 2021 Share Posted October 12, 2021 You've posted a few pieces of code without any context to how they are used. That makes it really difficult for anyone but you to know what you're talking about. If clickedD.php is where the AJAX request with the dentist's name is sending its data then only clickedD.php has the dentist's name available to use. The AJAX happens after the page loads, so PHP cannot time travel and take the information from it and retroactively change what happened when it was first creating the page. Without knowing more about what you're doing, I would say that you "have to" use clickedD.php and AJAX to modify the page with whatever information you want - information which would have to be returned by clickedD.php itself. But I suspect there's an easier method to do what you want. No way for me to know what it is, but I do believe there is one. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/313963-how-can-i-use-a-js-variable-as-a-php-variable/#findComment-1590956 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.