how to retrieve a javascript value in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

how to retrieve a javascript value in php

Post 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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I've no clue what framework this is. You should be more specific.
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

i 'm sorry you're right i'm using zend framework
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post 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]
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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)?
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

do a

Code: Select all

var_dump($_POST);
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

it shows the same error message
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

post the code, please.
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post 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]
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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);
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post 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:
Post Reply