HTML form to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

HTML form to php

Post by koola »

Hi,

I want to post some data into different catagorie flat file databases.
The html form should choose a database and then send the chosen value into the php post form and the data be inserted into the chosen db.

This is what I have so far in the php post file:

Code: Select all

$cat = $_POSTї'cat'];
}
if($cat = 1){
$filename = "data/dvd.db.txt";
}
else if($cat = 2){
$filename = "data/avi.db.txt";
}
else if($cat = 3){
$filename = "data/vcd.db.txt";
}
else if($cat = 4){
$filename = "data/svcd.db.txt";
}
In the html form:

Code: Select all

<tr>
    <td width="172" height="20" align="right">Catagory:</td>
    <td width="412"><select name="cat" class="txtbox">
      <option value="1" selected>DVD</option>
      <option value="2">AVI</option>
      <option value="3">VCD</option>
      <option value="4">SVCD</option>
    </select></td>
  </tr>
Can someone please help us out. Thanks :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

what's the <form> tag look like?
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

Hi patrikG,

The form tag:

Code: Select all

<form action="post.php" method="post" name="post">
This is the php post form that should post into the chosen db:

Code: Select all

<?

$author = $_POST&#1111;'author'];
$title = $_POST&#1111;'title'];
$tools = $_POST&#1111;'tools'];
$url = $_POST&#1111;'url'];
$cat = $_POST&#1111;'cat'];

if($cat = 1)&#123;
$filename = "data/dvd.db.txt";
&#125;
else if($cat = 2)&#123;
$filename = "data/avi.db.txt";
&#125;
else if($cat = 3)&#123;
$filename = "data/vcd.db.txt";
&#125;
else if($cat = 4)&#123;
$filename = "data/svcd.db.txt";
&#125;
$author = htmlspecialchars(trim(stripslashes($author)));
$title = htmlspecialchars(trim(stripslashes($title)));
$url = htmlspecialchars(trim(stripslashes($url)));
$date = date('d M Y');

$guideurl = "<a href='$url' target='_blank'>View</a>";

// Build db string
$news = $title."|".$guideurl."|".$tools."|".$date."|".$author."\n" ;

$data = fopen($filename, "a");
fwrite($data, $news);
fclose($data);

header("location: $HTTP_REFERER ");

?>
Thanks for the reply :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

And what is the problem?
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

The problem is that it doesn't post into the selected db.
I don't know why it doesn't, could it be my code?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Put

Code: Select all

echo "<h4>CAT</h4><pre>";print_r($_POST["cat"]);echo"</pre>";
at the top of your post.php page and post the results here.
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

Posting with DVD gives:

Code: Select all

CAT
1
Posting with AVI:

Code: Select all

CAT
2
Posting with VCD:

Code: Select all

CAT
3
Posting with SVCD:

Code: Select all

CAT
4
It looks like the chosen value is being passed, but the code is not selecting the correct db :( No idea why?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Isn't it? You're writing the SQL string to a file - have you looked at the contents of that file?
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

Nope :(

I'm putting the data into a text file db, but not the one I selected.
I've looked in all of the files, but all the data is being put into 'dvd.db.txt' and not the chosen one.

Any ideas as I am real stuck with this one?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Try

Code: Select all

$data = fopen($filename, "a+");
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

I carn't belive it! :evil:

It still doesn't insert the data into the correct db.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Check the directory permissions, start chmodding them to 0777 and see if that helps.
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

Yep, they are all chmodded at 777 but it is still not selecting the correct db?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

echo $title before or after you write to the file.
User avatar
koola
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 4:02 am
Location: Leeds, UK

Post by koola »

Ok, I echoed them all, this what was displayed:

Author: koola
Title: test
Tools: test
URL: Http://test.com
Cat: 1

PHP code:

Code: Select all

<?php
echo "Author: $author<br>";
echo "Title: $title<br>";
echo "Tools: $tools<br>";
echo "URL: $url<br>";
echo "Cat: $cat<br>";
?>
Post Reply