Forum Moderators: coopster

Message Too Old, No Replies

Arrays Javascript / PHP

         

Daxman23

11:57 am on Nov 7, 2007 (gmt 0)

10+ Year Member



Hey again all!

I wrote this data array in javascript, it is an automated 3 level dropdown menu. If i want to store the chosen options in a database..what code do i use?

As you might have noticed from my other post im not an PHP expert so any help or tips are appriciated.

Thanks a lot!

Dax

var arrItems1 = new Array();
var arrItemsGrp1 = new Array();

arrItems1[3] = "Påminnelse";
arrItemsGrp1[3] = 1;
arrItems1[4] = "Inkasso";
arrItemsGrp1[4] = 1;
arrItems1[5] = "Inbetalning";
arrItemsGrp1[5] = 1;

arrItems1[6] = "Täckning";
arrItemsGrp1[6] = 2;
arrItems1[7] = "A.P.N.";
arrItemsGrp1[7] = 2;
arrItems1[8] = "Blackberry";
arrItemsGrp1[8] = 2;

arrItems1[0] = "Kontantkort";
arrItemsGrp1[0] = 3;
arrItems1[1] = "Tjänster";
arrItemsGrp1[1] = 3;
arrItems1[2] = "Abg Byte";
arrItemsGrp1[2] = 3;

var arrItems2 = new Array();
var arrItemsGrp2 = new Array();

arrItems2[21] = "Förfrågan";
arrItemsGrp2[21] = 0
arrItems2[22] = "Klagomål";
arrItemsGrp2[22] = 0
arrItems2[28] = "Tvist";
arrItemsGrp2[28] = 0

PHP_Chimp

12:40 pm on Nov 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



assuming you are using a mysql database you need to have a look through the mysql [php.net] functions.
There are a load of examples for putting data into a database.

Which of your variables do you want to store and what will the table look like you are storing these variables in?

Is this something to allow you to look through the results, or is this just for the user to use as a default setting for something? As if this is for a default setting it may well be a lot easier to store the information as a cookie [php.net].

Daxman23

1:03 pm on Nov 7, 2007 (gmt 0)

10+ Year Member



Thanks for a prompt reply.

This data is to be entered by different people and is there only to b viewed by one with a mysql query.

The mysql table looks like this:

team works fine
agent works fine

cat1 = js array
cat2 = = js array
cat3 = = js array

The query to view the results is to be written later or perhaps not at all, the most important thing is to get the data input to work.

Will check the links 2:)

phranque

1:18 pm on Nov 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, dax!

you can't do much with a javascript array on the server.
you will have to send the required data either as parameters in the query string of a HTTP GET or as form parameters in a HTTP POST.
that will make the data available to a php script on the server which is referred to either in the url of the GET or the form action attribute of the POST.
then you should look into php input parameter processing to retrieve your parameter values and then you can figure out how to store the data.

PHP_Chimp

1:35 pm on Nov 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP and javascript code tends to be quite similar in nature, so you could alter your javascript arrays to be php arrays, or send the information using ajax.

The mysql table looks like this:

team works fine
agent works fine

cat1 = js array
cat2 = = js array
cat3 = = js array


Am I write in assuming that you table columns are called -
team, agent, cat1, cat2, cat3

How do your arrItems1 and arrItmesGrp1 fit into this table? As you have 5 columns and 2 bits of data you are inserting?

Daxman23

1:37 pm on Nov 7, 2007 (gmt 0)

10+ Year Member



The 2 bits of data that i am inserting are supposed to go into cat2 and cat3, the other tables are configured just the way i want them to be.

Im sorry for being a bit unclear but got work to do 2 :)

phranque

1:38 pm on Nov 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i see 4 items - there is also arrItems2 and arrItmesGrp2

PHP_Chimp

1:53 pm on Nov 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So once you get the data into the server, via ajax if you dont want to change all of the code to php then you need a query that looks something like -

/*
assuming you have got your arrItems1 and arrItemsGrp1 into the
server and named then the same thing.
You also need to change <table_name> to the name of your table ;)
*/
$safeItems1 = [url=http://uk3.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]($arrItems1);
$safeItemsGrp1 = mysql_real_escape_string($arrItemsGrp1);
$q = "INSERT INTO <table_name> (cat2, cat3) VALUES ($safeItems1, $safeItemsGRP1);";
$result = mysql_query($q);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// do what ever you want with the results

That mysql query may be wrong, as mysql is not a database that i use...but it should get you close. If the query is wrong im sure someone will point it out ;)
Make sure that you escape the query. You may have to use stripslashes [uk3.php.net] to remove added slashes if the magic_quotes_gpc is set on.

if ([url=http://uk3.php.net/manual/en/function.get-magic-quotes-gpc.php]get_magic_quotes_gpc[/url]()) {
stripslashes($data);
}