Click to See Complete Forum and Search --> : Changing Variables in a Form
jeffy777
03-04-2005, 10:00 AM
Hello,
I have a script on the main page of my site that uses variables that are different text messages to be displayed when a visitor mouses over various areas of the page. I'm wondering how I can set it up so that these varibles can be edited in a form on a separate admin-type page. The data can be stored in either a txt file or a MySQL database.
Can someone steer me in the right direction? Thank you for your time :)
phpnovice
03-04-2005, 11:17 AM
Show what your code and html look like for displaying this information.
jeffy777
03-04-2005, 12:27 PM
OK, I'm using a javascript in the head, here's an example of part of it:
messages=new Array()
messages[0]="Welcome to our website!"
messages[1]="Click this link to return to our main site!"
messages[2]="Click this link to view our calendar of upcoming events!"
messages[3]="Click this link to find out more about us!"
That's just the little part of the code that sets up the text, there's more code that defines mouseover functions and stuff. Then down in the body in the links themselves I have mouseover commands telling which parts of text display for the different links.
I would like to have a form where the text messages can be defined as variables and stored. So the above code would look something like this:
messages=new Array()
messages[0]="$text1"
messages[1]="$text2"
messages[2]="$text3"
messages[3]="$text4"
And it would still display the same messages, but it would be easy to update from the form.
phpnovice
03-04-2005, 12:37 PM
Yes, you can create a `messages` table and loop through it to produce that JavaScript array.
<?php
echo 'messages=new Array();';
$i = -1;
while ... :
$i++;
echo "messages[{$i}]=\"{$rst['msgtxt']}\";";
endwhile;
?>
jeffy777
03-04-2005, 12:48 PM
so how do I set up a form where I can type the text in fields and then have it inserted into a table? And then how do I retrieve it? While I have worked with a lot of pre-made php scripts, I've never done something like this from scratch, so please be specific or point me to a tutorial that explains it.
phpnovice
03-04-2005, 01:55 PM
So, no MySQL experience? Is that what you're telling me? :)
jeffy777
03-04-2005, 04:09 PM
I do have SQL experience, but it's basically just insalling scripts from sites like HotScripts.com, stuff where you just have to run a sql file that automatically sets up the tables and stuff :)
So I'd really like to know how to store form results in a MySQL table and then retrieve them on a separate php page. Can you help :) I really appreciate any help that can be offered.
phpnovice
03-04-2005, 08:11 PM
Well, I use these two little functions for standard SQL execution and error handling/reporting. You may or may not wish to use them.
function report_error($sql) {
echo '<p>Error: '.mysql_errno().'; '.mysql_error()."<br />\nsql = \"{$sql}\"</p>\n";
}
function execute_sql($sql) {
if (!$result = mysql_query($sql)):
report_error($sql);
return false;
endif;
return $result;
}
Then, either your ISP or you need to create a MySQL database for you to use with your site. Once that is created, you can connect to it -- for example -- as follows:
$dbname = 'mysqldb';
$sql = "Connect {$dbname}";
if (!$conn = mysql_connect('localhost', 'usr', 'pwd')):
report_error($sql);
exit;
endif;
mysql_select_db($dbname);
Next, you can create your messages table -- for example -- as follows:
$sql = "Create Table `Messages` (
`msg_id` INT NOT NULL AUTO_INCREMENT,
`msg_txt` VARCHAR(255) NOT NULL,
PRIMARY KEY (`msg_id`)
) COMMENT = 'Messages Table';";
if (!$rst = execute_sql($sql)):
mysql_close();
exit;
endif;
With me so far?
jeffy777
03-05-2005, 05:25 PM
Yes, I'm with you. I don't undestand every line of the code, but I can tell what happens. I already have a database set up because I have quite a few scripts running on the site (phpbb2, polls, etc.) and I have full access to phpmyadmin. I installed and edited these scripts, but like I said I haven't made any php scripts from scratch.
So this code would create the table called 'Messages', connect to the database, and check for errors.
Would the next step be to store the form results in the table?
I really really really appreciate your help!!!
jeffy777
03-06-2005, 03:20 PM
Ok, I have the 'messages' table in the database and the text messages are now in the table. I also have a way to update this through a form on my site using a free and cool tool called AppGini.
But now I'm wondering how to retrieve this info from the database and turn each text message into a variable?
So I guess what I need to know is this: What code do I need to connect ot the database and then get the info from the 'messages' table and then assign the varibles?
thanks!
phpnovice
03-06-2005, 04:37 PM
Use code something like this to build the JavaScript data array:
var msg_array = new Array();
<?php
$sql = "Select * from `Messages` Order By 1";
if (!$rst = execute_sql($sql)):
mysql_close();
exit;
endif;
while ($row = mysql_fetch_row($rst)):
echo "msg_array[{$row[0]}] = \"{addslashes($row[1])}\";";
endwhile;
mysql_free_result($rst);
?>
jeffy777
03-06-2005, 05:15 PM
thanks for all your help, it seems to be working great at this point :)
phpnovice
03-06-2005, 10:56 PM
Cheers.