rpjd Posted February 28, 2011 Share Posted February 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
samoht Posted February 28, 2011 Share Posted February 28, 2011 not sure I understand your issue. Are you sending the values of a form using a submit button? Maybe you could post some code as an example? Quote Link to comment Share on other sites More sharing options...
Zane Posted February 28, 2011 Share Posted February 28, 2011 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 Quote Link to comment Share on other sites More sharing options...
rpjd Posted March 1, 2011 Author Share Posted March 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 1, 2011 Share Posted March 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
rpjd Posted March 1, 2011 Author Share Posted March 1, 2011 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.' Quote Link to comment Share on other sites More sharing options...
samoht Posted March 1, 2011 Share Posted March 1, 2011 Why is your textarea an array value? try <td><textarea name="text">Today is saturday, no work today.</textarea></td> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2011 Share Posted March 1, 2011 Multiple textarea's with a array name works (just tested), so there must be something going on in your actual code, either in the form or in the form processing. Quote Link to comment Share on other sites More sharing options...
rpjd Posted March 1, 2011 Author Share Posted March 1, 2011 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? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2011 Share Posted March 1, 2011 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? Quote Link to comment Share on other sites More sharing options...
rpjd Posted March 1, 2011 Author Share Posted March 1, 2011 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? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2011 Share Posted March 1, 2011 Just display the text as text, but the actual text you use is determined solely on the server based on which checkbox was checked. You have got all the different possible text stored in a database table or somewhere now? Quote Link to comment Share on other sites More sharing options...
rpjd Posted March 1, 2011 Author Share Posted March 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.