Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variable to PHP from Javascript 1

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

I am putting together the following rank polling for my site...


The entire code may be viewed here...


The following method to assign the value of the javascript variable to PHP before writing to the database is not working.

$bat0 = $_GET['Batsman[0]'];
$bat1 = $_GET['Batsman[1]'];
...etc...

Any input is greatly appreciated...

Thanks,
Arun
 
[ol]
[li] I don't see any form inputs with the name Batsman[x]. Since only form inputs like text boxes get submitted with a form and populate the GET variable there's no way that can work. [/li]

[li]If you had form elements with those names, the correct way to access the array would be:

$_GET['Batsman'][0]
$_GET['Batsman'][1]
....
[/li]
[li]Your form is using the post method:
Code:
<form name="Batsman" method="[red]post[/red]" ...

So values submitted would be in the POST variable rather than GET.
$_POST['Batsman'][x]

[/li]
[/ol]

Personally I would simply turn your Javascript array into a string, set it as a value of a hidden form element so its submitted with the form to the PHP script. Then use PHP if you need to, to turn it back into an array for you to use.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Phil, working on changes...
Arun
 
Hello,

Continuing the above effort, the full code may be seen at...

The polling site is...

The javascript variable, 'Batsman', is a string and will have a default value of:

"A B C D E F G H I J"

As the ranking are modified via the form, the string value is changed.

In order to pass this variable to PHP, where do i place the hidden input form element?

Help is much appreciated...
Thanks,
Arun
 
Anywhere within your form tag. Once you submit the page to your PHP script, the POST array variable will be populated with the value you assign to the hidden input.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hello Phil,

Thanks for your input. Got things working with the following...

I have the following javascript code...
document.getElementById('BATS').value = Batsmen;

and the following input html tag...
<input id="BATS" type="hidden" name="userchoice">

and the following MySQL insert via PHP...
INSERT INTO $table (Email, Choice) VALUES ('$_POST', '$_POST[userchoice]')

Appreciate it,
Arun
 
Glad it worked for you

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top