Jump to content

Archived

This topic is now archived and is closed to further replies.

crzyman

Passing values using a form *SOLVED*

Recommended Posts

This is the form:
[code]
<?
$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>
[/code]

This is the php script test.php:
[code]
<?
//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')");
}
?>
[/code]

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.
[code]
<?
$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>
[/code]

Share this post


Link to post
Share on other sites
$artist_name =  [b]$_GET[/b]['artist'];

Your form method is [b]POST[/b] :)

Retrieve ALL the data from the form from the $_POST array. Do NOT rely on variables being global as your code assumes.

Share this post


Link to post
Share on other sites
Thanks for your help. The value for artist gets entered into the database as .$myValue. and not as "Joe"? Please advise. Many thanks.

Share this post


Link to post
Share on other sites
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:

[code]<?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);
}
?>[/code]

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 [b]display[/b] in your preferred format.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.