Jump to content

Archived

This topic is now archived and is closed to further replies.

Tumaini

Saving a select array to a file as a string

Recommended Posts

Greetings!
This is my first post here, and I'm so glad to have found a place with helpful people, who don't call you an idiot for not knowing something! :)

Here's my problem:
I'm trying to save a select array to a file as a string (joining the entries in the array with '§').
I tried alot of things, before someone finally told me that I can't use js-variables in php code.
So I tried to find out how you turn a js-variable/array into a php-variable/array.
I found $_POST, but when I use:

[code]<?php
$saving = $_POST['turnelista'];
echo $saving;
?>[/code]

where 'turnelista' is the name of the select list in the form that has the method="post" and action="spara.php", which is the page that has the above php-code, it echoes nothing. Seems you can't use $_POST to get a php-array out of a select form?

If you have an explanation for this, please let me know.
If you have any better ideas of how to save the info from a select list into a single string on a file, each entry seperated by §, please let me know!
Thanks in advance!

Share this post


Link to post
Share on other sites
If $_POST['turnelista']; is actually an array, you're going to have to loop through it.
[code]
$mystr = array();
foreach($_REQUEST['turnelista'] as $key)
    {
        $mystr[] = $key;
    }
echo implode(',', $mystr);
[/code]

Share this post


Link to post
Share on other sites
Ok, let's say I want to do it this way:
When the submit button is clicked, a javascript function is run, where the select array entries are joined together, seperated by § (done).
Then the js variable which stores this "joined array" is converted to a php variable - how do I do this?
Then it's saved (done using fopen, fwrite, and fclose).

So, how do i convert a js variable to a php variable?

Share this post


Link to post
Share on other sites
Untried suggestion :

create a hidden field in the form
[code]
<input type='hidden' name='selectList' value=''>[/code]

And in your js function,
[code]
document.formname.selectList.value = joined_array;[/code]

You should then be able to pick up the values in $_POST['selectList']

Share this post


Link to post
Share on other sites
Thanks for the answers. I would try it, but now the line I used for loading the file has broken. I can't see what's wrong.
Is anything wrong with this line itself?
[code]var spelning = '<? echo get_file_contents('spelning.txt') ?>'.split('§');[/code]
It used to work just fine, but now it makes my page go totally blank if it's included, and half an hour ago the same line gave a "spelning not declared" later down in in the code (btw, there are underscores in "get_file_contents").
Should I load the file some other way?

Actually, that doesn't seem to be the problem, as if I define it by putting in values directly, it still says it's undefined. Will have to investigate (again).

Share this post


Link to post
Share on other sites
Well, I found the extra error that made it undefined...I'd forgotten two single quotes around a character that messed everything up. That's working now. Time to try and get the saving working as well.

Share this post


Link to post
Share on other sites
this worked

[code]
<SCRIPT>
var text = "<? echo file_get_contents('spelning.txt') ?>"
var spelning = text.split('§');
alert (spelning[0]+"\n"+spelning[1]+"\n"+spelning[2]);
</SCRIPT>[/code]

Share this post


Link to post
Share on other sites

×

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.