Jump to content

How can I use a JS variable as a PHP variable?


Elara

Recommended Posts

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  image.thumb.png.a66028aa9019af51a5df0d670455661d.png

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

 image.png.87197f0ed0dd00611e787e76cb20bf34.png

what is wrong with my code ? please help me out 

Link to comment
Share on other sites

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.

  • Thanks 1
  • Haha 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.