Jump to content


Photo

Saving a select array to a file as a string


  • Please log in to reply
8 replies to this topic

#1 Tumaini

Tumaini
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 September 2006 - 04:12 PM

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:

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

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!

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 22 September 2006 - 04:19 PM

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

Info: PHP Manual


#3 Tumaini

Tumaini
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 September 2006 - 05:08 PM

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?

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 22 September 2006 - 05:28 PM

Untried suggestion :

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

And in your js function,
document.formname.selectList.value = joined_array;

You should then be able to pick up the values in $_POST['selectList']
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Tumaini

Tumaini
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 September 2006 - 07:07 PM

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?
var spelning = '<? echo get_file_contents('spelning.txt') ?>'.split('§');
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).

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 22 September 2006 - 07:27 PM

Try file_get_contents()
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 Tumaini

Tumaini
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 September 2006 - 07:28 PM

D'oh! Thanks! :D

#8 Tumaini

Tumaini
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 September 2006 - 07:33 PM

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.

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 22 September 2006 - 07:50 PM

this worked

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users