i have a php variable that holds a date and i want to pass to a javascript function, but what happens is that i get a junk value instead, what should i do?

$data = $_POST['datep'];
echo "<script language=javascript>fnClickAddRow($nplaca,$defeito,$posicao,$data);/script>";
<script type="text/javascript">
function fnClickAddRow(n__placa,defeito2,posicao2,dataf) {
	var lext = "<?php echo $l_ext ; ?>";
        $('#table1').dataTable().fnAddData( [	                              	
		                                 	lext,                                 	
		                                 	n__placa,
		                                 	defeito2,
		                                 	posicao2,
		                                 	dataf		                                 	
	]);
	}
</script>

everything except dataf prints fine on the table (datatables).

the $l_ext wasnt working either until i found this solution (echoing to the var), but doing the same for dataf doesnt work ($l_ext is GET instead of POST)

thanks

If you're using Firefox you can open a console that tells you the JavaScript errors. I would check that first.

Second, you should verify that the variable has the correct information before sending it to the JavaScript simply by printing it out wherever it's initialized.

hi, thanks for the response.

if i echo the #data on php i can see the date on the format i want (dd/mm/yyyy) but after it passes to javascript i can see only a string of numbers (0.00048958943893).

I also cant see any errors on firebug, but i started to use it today, so i might not be looking on the correct places.

thanks!

Through the process of elimination, the next thing I would do is add "alert(lext);" directly after "var lext = "<?php echo $l_ext ; ?>";" to see if the format is correct. If it's not, the error is somewhere here:

$('#table1').dataTable().fnAddData( [
lext,
n__placa,
defeito2,
posicao2,
dataf
]);

I don't know JavaScript so you may have to post on the JavaScript forum.

the lext works fine, i just added because i though it was strange that it worked

the lext is a number with a / , but the i get this variable through GET instead of POST.

the one that is not working is the $data variable.

i will post on javascript forum too, thanks!

I've always used $_REQUEST instead of post or get. It combines all of them so you're less likely to loose variables. There is some debate about security issues doing this, but I've never encountered any problems.

First, check to make sure the form field name matches the $_REQUEST name.
Second, make sure there is a value in the form field (I know it sounds obvious, but errors can hide in the obvious very effectively)

If the problem still persists, it's outside of my realm of knowledge.

tried with $_REQUEST and i still get the strange value, im sure its because the javascript is transforming the php variable to something else like a float.

the names and values are correct, it does get a value, it is just the wrong one =/

thanks

solved the problem, the PHP variable has to be inside single quotes to be passed as a text otherwise it will convert into a number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.