Page 1 of 2

how to retrieve a javascript value in php

Posted: Thu Aug 23, 2007 10:37 am
by nahydy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i'm trying to retrieve javascript parameter's value in php. So i created a function which send an array to my controllers. and i tried to do this:

Code: Select all

function formulaireAction()
{
	
	$this->view->title = "formulaire d'inscription";
	if ($this->_request->isPost()) 
	{
		$res=$this->_request->getPost('tabs_coords');
		$fields= explode(',',$res); 
		
		
		foreach ($fields as $v)
		{
			$this->view->message=$v;		
		}
		
		
		$this->render();
	}
}
and when i tested the value of my array are not dispayed. i make some test to make sure that the array that i'm sending is not empty. so i don't see where the problem is. can you help me?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Aug 23, 2007 10:47 am
by superdezign
I've no clue what framework this is. You should be more specific.

Posted: Thu Aug 23, 2007 10:50 am
by nahydy
i 'm sorry you're right i'm using zend framework

Posted: Thu Aug 23, 2007 11:01 am
by superdezign
You sure the problem is in the PHP? Have you tried print_r($_POST) to ensure that the values are coming in as expected?

Posted: Fri Aug 24, 2007 2:42 am
by nahydy
i tried print_r() and it displays nothing, i tested if my array was empty or not before submitting my form. and it wasn't empty. the problem is when i try to retrieve it seem that my array is empty. i don't know what's the problem. any idea

Posted: Fri Aug 24, 2007 6:53 am
by superdezign
The problems seems to be occurring well before the information is posted to PHP. Are you certain you are sending a post request?

Posted: Fri Aug 24, 2007 7:57 am
by nahydy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


yes i'm sure of sending a post request. i used a function which send an array in a hidden input


[syntax="javascript"]
function setValue()
{

document.getElementById("tabs_coords").value=tab.toString();

} 
tab is my array which wasn't empty. and created my form like this

Code: Select all


<form action="<?php echo $this->baseUrl ?>/index/formulaire/" method="POST" id="form1" onsubmit="javascript:SetValue();">
<input type="text" value="" name="idText" />

<div id="Canvas" style="position:realtive;height:5px;width:5px;">
	<img  alt="map_pixels" src="<?php echo $this->baseUrl;?>/public/images/main.png" style="position: relative; top: 70px; left: 30px; visibility: visible;" usemap="#main" width="1000" height="1000"/>
</div>


<input type="hidden"  name="tabs_coords"/>

<input type="submit" value="submit" id="submit"/>

</form>

and i tried to pass in my javascript function a simple string and it worked well. for the moment the problem is focused in how to send an array from javascript to php. is there any other solution to let me send an javascript array to php?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 24, 2007 8:29 am
by patrikG
nahydy wrote:i tried print_r() and it displays nothing, i tested if my array was empty or not before submitting my form. and it wasn't empty. the problem is when i try to retrieve it seem that my array is empty. i don't know what's the problem. any idea
Are you sure you're doing print_r($_POST)?

Posted: Fri Aug 24, 2007 8:41 am
by nahydy
it's kind bizarre this time print_r shows me this error
Function name must be a string in /var/www/test/pixels/application/controllers/IndexController.php on line 125
i don't understand the last time i used print_r doesn't cause any error and i'm sure that i have nothing changed since . i don't really understand what may cause this problem.

Posted: Fri Aug 24, 2007 8:43 am
by patrikG
do a

Code: Select all

var_dump($_POST);

Posted: Fri Aug 24, 2007 8:54 am
by nahydy
it shows the same error message

Posted: Fri Aug 24, 2007 9:00 am
by patrikG
post the code, please.

Posted: Fri Aug 24, 2007 9:22 am
by nahydy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok this is the code :


[syntax="html"]

<html>
<head>
<title><?php echo $this->escape($this->title); ?></title>
<script type="text/javascript">

tab1=new Array();



function remplir()
{
	
	tab1.push('na');
	tab1.push('yai');
	tab1.push('cee');
       
}
remplir();


function afficher_t()
{
	
	
	var arv = tab.toString();
	alert(arv);

}


function setValue()
{
var arv = tab1.toString();

document.getElementById("tabs_coords").value=arv;

} 



</script>

</head>


<body >

<table>
<tr>
<td>
<p>Map de pixels</p>
</td>
</tr>

<tr>
<td>

<form action="<?php echo $this->baseUrl ?>/index/formulaire/" method="POST" id="form1" onsubmit="setValue();">
<input type="text" value="" name="idText" />

<div id="Canvas" style="position:realtive;height:5px;width:5px;">
	<img  alt="map_pixels" src="<?php echo $this->baseUrl;?>/public/images/main.png" style="position: relative; top: 70px; left: 30px; visibility: visible;" usemap="#main" width="1000" height="1000"/>
</div>


<input type="hidden" name="tabs_coords"/>
<input type="button" name="afficher" value="afficher" id="afficher" onclick="javascript:afficher_t();" />
<input type="submit" value="submit" id="submit"/>

</form>

</td>
</tr>
</table>



</body>
</html>


and in my controllers i do this to get the array value:[/syntax]

Code: Select all

function formulaireAction()
{
	
	$this->view->title = "formulaire d'inscription";
	if ($this->_request->isPost()) 
	{
		
		//$res=$this->_request->getPost('tabs_coords');
		//$fields= explode(',',$res); 
		
		$this->view->message=$this->_request->getPost('tabs_coords');
		
		//var_dump($_POST('tabs_coords'));
		$this->render();
	}
}
without the var_dump it shows nothing.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 24, 2007 9:25 am
by patrikG
$_POST is an array. As such, it uses square brackets.

Code: Select all

var_dump($_POST('tabs_coords'));
should be

Code: Select all

var_dump($_POST["tabs_coords"]);
To see all posted values:

Code: Select all

var_dump($_POST);

Posted: Fri Aug 24, 2007 9:34 am
by nahydy
ok this was a fatal error . i tried again and i get this when i run my page:
string(0) ""
it seems that my String is empty. :oops: