Javascript Sending and using Parameters and Database

Soo… I’m a newb and need some help.
I know this is a lot of information, so thank you for your time =]

I’m trying to do a basic website in which I accept information via fields and then insert them into my database. I’ve got it to the point in which I can create a database via html and display data already in the database.

I have been using this tutorial to get me to where I’m at now:
http://www.freewebmasterhelp.com/tutorials/phpmysql/

Here is the code I’m using for the form section.:

[code]

Date: //
Title:
Location:
Other:
[/code]

From what I understand this (when submit is clicked) passes the information via a url to my newsInsert.php page. As I get a url that looks like this when i click it: (filled out with 01 01 0001 Test Test Test)
http://infinitysocial.net/newsInsert.php?day=01&month=01&year=0001&title=Test&location=Test&other=Test

So essentially I need help getting that information from there into some java script so I can use the variables in my php to put it in my database.

Here is what I’ve done so far on my newsInsert.php page.

[code][/code]

[php]<?

//the below section contains password information to log into my datbase =/ just fyi
include(“NewsDB.AiNFO.php”);

$date=$_POST[‘date’];
$title=$_POST[‘title’];
$location=$_POST[‘location’];
$other=$_POST[‘other’];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

$query = “INSERT INTO news VALUES (’’,’$date’,’$title’,’$location’,’$other’)”;
mysql_query($query);

mysql_close();
?>[/php]

I don’t know how to associate the javascript variables with variables inside my php. And I don’t know if my parameters the way they are set up with the forum actually are getting the data or not.

And finally I’m confused by some of the php code:
[php]$query = “INSERT INTO news VALUES (’’,’$date’,’$title’,’$location’,’$other’)”;
mysql_query($query);[/php]

^How does this get into my database? I see that it uses variable $query to put in all the other variables into one… and then we call that variable $query with mysql_query()? sooo… it’s saying to run mysql_query() which is php to say to run something as SQL i assume? and the INSERT INTO news Values will put that data () into my database? Sound about right? The reason i ask is cause I was trying to use sample code prior to this to put in information into my news DB and it wasn’t going in. Is there something missing in the php section?

I figure I better include this in case it becomes important for an answer or anyone else trying to learn from this question. This is the php i used to set up my table in my news DB:
[php]<?php
//Creates a “news” Table in News Database if its not there, with the fields.

include(“NewsDB.AiNFO.php”);
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=“CREATE TABLE news (id int(100) NOT NULL auto_increment,date varchar(10) NOT NULL,title varchar(255) NOT NULL,location varchar(255) NOT NULL,other varchar(255) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))”;
mysql_query($query);
mysql_close();

?>[/php]

Just a follow up thought. Is it even required that I use javascript to get these variables from the forum? Or can I do it directly with php?

got the same problem… if someone could reply it asap… :-X

Man I’m so embarrassed looking at my question now. I was such a novice back then.

To answer my question, the easiest way I found to get the java script variables into php variables so I can put them in my database is to pass them through a form. There are several ways to do that.
One way I did it was just to simply send them in a url via javascript.

for example:

that code will redirect to yourPage.php and will contain those variables information.
You then need to get them from the url via php.

you could do so likewise:
[php]<?php
// Note: $_REQUEST you may use other methods besides request if you want.
$gotA=$_REQUEST[‘someNameA’];
$gotB=$_REQUEST[‘someNameB’];
?>[/php]

At this point, gotA would be equal your former javascript variable somethingA and same for B.

You could also do this:

[code]

[/code]

[php]<?php
$getAlpha = $_REQUEST[‘alpha’];
echo “$getAlpha”;
?>[/php]
After submitting the form it will print out the number 1.

This is a cool way of doing it to:

[code]

[/code]

[php]<?php
$getAlpha = $_REQUEST[‘alpha’];
echo “$getAlpha
”;
?>[/php]

After submitting the form it will print out the number 1.

using

Can it also pass string variables ???

Yes.

thanx …

Sponsor our Newsletter | Privacy Policy | Terms of Service