Author Topic: Javascript Sending and using Parameters and Database  (Read 112 times)

anaror

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Javascript Sending and using Parameters and Database
« on: January 26, 2012, 04:24:13 PM »
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: [Select]
<!--Input Table-->
<form action="newsInsert.php" method="postNews">
Date: <input type="text" name="day" hspace="2" maxlength="2">/<input type="text" name="month" hspace="2" maxlength="2">/<input type="text" name="year" hspace="4"  maxlength="4"><br>
Title: <input type="text" name="title" maxlength="255"><br>
Location: <input type="text" name="location" maxlength="255"><br>
Other: <input type="text" name="other" maxlength="255"><br>
<input type="Submit" value="Submit">
</form>   

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: [Select]
<script language="JavaScript">

function postNews(dateR, titleR, locationR, otherR)
{
var date = 'dateR';
var title = 'titleR';
var date = 'locationR';
var date = 'otherR';
}

</script>


PHP Code: [Select]
<?

//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();
?>


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 Code: [Select]
$query "INSERT INTO news VALUES ('','$date','$title','$location','$other')";
mysql_query($query);


^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 Code: [Select]
<?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();


	

?>





anaror

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Javascript Sending and using Parameters and Database
« Reply #1 on: January 26, 2012, 04:30:25 PM »
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?