Jump to content

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!
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]
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?
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']
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).
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.