Jump to content


Photo

Passing values using a form *SOLVED*


  • Please log in to reply
4 replies to this topic

#1 crzyman

crzyman
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 15 September 2006 - 02:25 PM

This is the form:
<?
$myValue = "Joe";
?>
<form action="test.php" method="post">
<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100'>
<input type="hidden" name="artist" value="<?php echo $myValue; ?>">
<input type="submit" name="submit" value="submit">
</form>

This is the php script test.php:
<?
//the host, name, and password for your mysql
mysql_connect("localhost","name","pass");

//select the database
mysql_select_db("news");

$artist_name =  $_GET['artist'];

if($submit)
{
   $time=date("h:ia d/j/y");

   $result=MYSQL_QUERY("INSERT INTO shoutbox (id,artist_name,name,message,time)".
      "VALUES ('NULL','$artist_name','$name','$message','$time')");
}
?>

Every thing gets put into the database except for the value of $name. Can someone please tell me what I am doing wrong? Many thanks.
I aslo tried this, but still no luck.
<?
$myValue = "Joe";
?>
<form action="test.php" method="post">

<INPUT TYPE='TEXT' value='name' NAME='name' SIZE=30 maxlength='100'><br>
<INPUT TYPE='TEXT' value='message' NAME='message' SIZE=30 maxlength='100'>
<input type="hidden" name="artist" value=".$myValue.">
<input type="submit" name="submit" value="submit">
</form>


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 02:31 PM

$artist_name =  $_GET['artist'];

Your form method is POST :)

Retrieve ALL the data from the form from the $_POST array. Do NOT rely on variables being global as your code assumes.
Legend has it that reading the manual never killed anyone.
My site

#3 crzyman

crzyman
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 15 September 2006 - 03:42 PM

Thanks for your help. The value for artist gets entered into the database as .$myValue. and not as "Joe"? Please advise. Many thanks.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 04:00 PM

I don't understand the line in the form code that defines $myName as Joe, but here's how I'd do the script that adds to your database:

<?php
//the host, name, and password for your mysql
mysql_connect("localhost","name","pass");

//select the database
mysql_select_db("news");
$name =  trim(strip_tags($_POST['name']));
$message = trim(strip_tags($_POST['message']));
$artist_name =  trim(strip_tags($_POST['artist']));

if(isset($_POST['submit'))
{
   $time = date("h:ia d/j/y");
   $query = "INSERT INTO shoutbox (id,artist_name,name,message,time)".
      "VALUES ('NULL','$artist_name','$name','$message','$time')";
   $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
}
?>

As a side note, you're making future trouble for yourself by entering the time in that format because is you ever want to sort by date, or compare dates you'll have big problems.  Use the datetime yyyy-mm-dd hh:mm format for time in going into the database. You can always reformat that to display in your preferred format.
Legend has it that reading the manual never killed anyone.
My site

#5 crzyman

crzyman
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 15 September 2006 - 06:11 PM

Thank you for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users