Jump to content

retrieving javascript array in php


rpjd

Recommended Posts

As part of a form submission, I have several textboxes  The textboxes have an array name such as text[].  I'm sending the textbox values via http request.  Sending an element value via http request is usually be done using document.getElementById, assigning the elements ID to a var and posting the var name .  Then accessing the value using $_POST.  However I'm sending an array of string values.  How do I access the array using $_POST?  Any help appreciated.

Link to comment
Share on other sites

If you're sending an array through POST then you have to access it like an array

 

For instance, you have a list of checkboxes in an array called checks[].  On your POSTED page you will have a variable called $_POST['checks']

 

You can access the elements of that array, just like you would any other array

$_POST['checks'][0] == first checked value

$_POST['checks'][1] == second checked value

$_POST['checks'][2] == third checked value

$_POST['checks'][3] == fourth checked value

$_POST['checks'][4] == fifth checked value

Link to comment
Share on other sites

Thanks Zanus, I'm submitting both and array of checkboxes and string values.  The checkboxes are submitting no hassle, its the text string values in textboxes that I'm trying to access in php.  Where 'text' is the name of the array being posted via http request,

$_POST['text'][$i]

is giving me undefined index.  I also tried

 $var = $_POST['text'];

which gave me undefined variable 'text'.  Frustrating to say the least!  Hope I'm going about this the right way.

Link to comment
Share on other sites

If

$var = $_POST['text'];

 

is telling you that $_POST['text'] is not defined then, guess what, it is not defined. Either the data is being sent with a different name than you think or it is not being sent. You should do a print_r($_POST) to verify what data is in the POST data. If the data is there, but with a different name/structure than you think then you can make the necessary adjustments. If the data isn't there, then the problem is likely in your form. Perhaps the text fields are not inside the start/end FORM tags.

 

But, some simple debugging will point you in the right direction. When you have problems such as these add some logic to display any variables to the page to validate what you are getting and what you are doing with it.

Link to comment
Share on other sites

My form table structure is as an example

<tr>
<td><input type='checkbox' name='checkbox[]' value='1'></td>
<td>x</td>
<td>y</td>
<td>z</td>
<td><textarea name='text[]'>Today is saturday, no work today.</textarea></td>
</tr>

I'm submitting both checkbox[] and text[].  checkbox works fine.  text[] is sent via http request as the textarea doesn't have a value like checkbox. 

I did

$text =  print_r($_POST);

which gave me:

Array ( [text] => Array ( [0] => 1 ) ) 1

as checkbox '1' was the only checkbox checked.  I have confirmed using an alert before being sent that 'text' contains the string

'Today is saturday, no work today.'

Link to comment
Share on other sites

I found the problem.  Because the text string is in a textbox, I didn't want the user to be able to delete/modify the text, as I it were contained within a table data cell for example.  So I had disabled='true' within the texarea tag.  When I did a print_r on $_POST, the text array was not in it and therefore not defined.  So I got rid of the disabled='true', now $_POST does contain the text array, but the user can now modify/delete the text, which I don't want.  Is there a way of getting $_POST to recognise the text, but without the user having access to delete/modify the text?

Link to comment
Share on other sites

I had disabled='true' within the texarea tag

When you don't post the actual code/html/data that exhibits the problem, no one here can help you.

 

The user can modify anything and everything you pass through a form. You cannot trust any input that comes from the user. What exactly are you trying to accomplish?

Link to comment
Share on other sites

My form table structure is

<tr>
<td><input type='checkbox' name='checkbox' value='x-y-z'></td>
<td>x</td>
<td>y</td>
<td>z</td>
<td><textarea name='text'>Text goes here</textarea></td>
</tr>

The only user input is the checkbox.  The text is displayed by the website, not inputted by the user.  So if the user selects a certain checkbox, its value and associated text is posted to and dispayed on another page.  The text could be a few words or a paragraph.  Is there a way of displaying and posting the text without a user being able to modify it? 

Link to comment
Share on other sites

No the text is not stored anywhere else, just displayed in a table within a form.  So if the checkbox associated with the text is checked by the user, the checkbox value along with the text are submitted and posted/displayed on another page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.