0

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)

if i echo the $data on php (before calling the javascript function) 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).


thanks

2
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by Airshow
0

Drako12,

Try it like this ...

<script type="text/javascript">
var nplaca = <?php echo $nplaca; ?>
var defeito = <?php $defeito; ?>
var posicao = <?php $posicao; ?>
var data = <?php $_POST['datep'] ?>
fnClickAddRow(nplaca,defeito,posicao,data);
</script>

If it still gives trouble then use your browser's "view source" to see what is actually in the served page.

Airshow

0

i didnt understand where/how i should use this code, maybe i explained my problem poorly.

the first part is on a PHP code where i get some values into variables after a submit and i call a javascript function with those variables into it.

$posicao = $_POST["posicao2"];
$defeito = $_POST["defeito2"];
$nplaca = $_POST["n_placa"];
$data = $_POST['datep'];

echo "<script language=javascript>fnClickAddRow($nplaca,$defeito,$posicao,$data);</script>";

the second part is the actual javascript code, which has jquery functions to add a row to a table (datatables)

<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>

the problem im seeing is that the PHP variable $data holds a date in a format dd/mm/yyyy and when its passed to the javascript function it turns into a float number (ie: 0.00003459874353897).


thanks for the help

0

i just tested something, nothing other than integers can be passed, if i try to pass a text on the input i get XX (being the value) is not defined.

why is that?

0

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.

0

That's because text values must be in singel or double quotes.

Going back to my earlier suggeston and assuming all the values to be text :

<script type="text/javascript">
var nplaca = "<?php echo $_POST["n_placa"]; ?>"
var defeito = "<?php $_POST["defeito2"]; ?>"
var posicao = "<?php $_POST["posicao2"]; ?>"
var data = "<?php $_POST['datep'] ?>"
fnClickAddRow(nplaca,defeito,posicao,data);
</script>

Here, you are building javascript statements by inserting php values into HTML/javascript text-strean that forms the served page. All parts that are not inside <?php ... ?> delimiters are just hard-coded javascript in the same way you use php to build HTML.

If any of the values is not text then simply remove the quotes.

Airshow

Edited by Airshow: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.