Tumaini Posted September 22, 2006 Share Posted September 22, 2006 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! Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/ Share on other sites More sharing options...
ober Posted September 22, 2006 Share Posted September 22, 2006 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] Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96816 Share on other sites More sharing options...
Tumaini Posted September 22, 2006 Author Share Posted September 22, 2006 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? Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96843 Share on other sites More sharing options...
Barand Posted September 22, 2006 Share Posted September 22, 2006 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'] Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96850 Share on other sites More sharing options...
Tumaini Posted September 22, 2006 Author Share Posted September 22, 2006 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). Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96891 Share on other sites More sharing options...
Barand Posted September 22, 2006 Share Posted September 22, 2006 Try file_get_contents() Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96911 Share on other sites More sharing options...
Tumaini Posted September 22, 2006 Author Share Posted September 22, 2006 D'oh! Thanks! :D Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96912 Share on other sites More sharing options...
Tumaini Posted September 22, 2006 Author Share Posted September 22, 2006 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. Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96918 Share on other sites More sharing options...
Barand Posted September 22, 2006 Share Posted September 22, 2006 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] Link to comment https://forums.phpfreaks.com/topic/21683-saving-a-select-array-to-a-file-as-a-string/#findComment-96925 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.