PDA

View Full Version : Problem using php functions in a form.



jetalomar
June 6th, 2002, 14:48
i'm having problem submiting a form using functions.

here is the code. i'm using include.

here is the code in the header.inc.php


// news form

function createnews() {

echo"<form enctype=\"multipart/form-data\" action=\"\" method=post>
<table width=\"31%\" border=\"0\">
<tr>
<td width=\"39%\"><b>Username</b></td>
<td width=\"61%\">
<input type=\"text\" name=\"username\">
</td>
</tr>
<tr>
<td width=\"39%\"><b>Password</b></td>
<td width=\"61%\">
<input type=\"password\" name=\"pass\">
</td>
</tr>
<tr>
<td width=\"16%\"><b>Subject</b></td>
<td width=\"84%\">
<input type=\"text\" name=\"subject\">
</td>
</tr>
<tr>
<td width=\"16%\"><b>Where</b></td>
<td width=\"84%\">
<select name=\"newstype\">
<option value=\"main\" selected>Main Page</option>
<option value=\"inter\">Interviews</option>
</select>
</td>
</tr>
</table>
<p>&nbsp;</p><table width=\"75%\" border=\"0\">
<tr>
<td colspan=\"2\">
<textarea name=\"article\" cols=\"60\" rows=\"20\"></textarea>
</td>
</tr>
<tr>
<td colspan=\"2\">
<div align=\"center\">
<input type=\"file\" name=\"file\" size=\"30\">
<br>
<input type=\"hidden\" name=\"news\" value=\"ok\">
<input type=\"submit\" name=\"Submit\" value=\"Submit\">
</div>
</td>
</tr>
</table></form> ";


}






// insert news in the database
function postnews()
{

$uses = mysql_query("SELECT * FROM users3 where eaname='$username' AND pass='$pass'",$db);

if(($usertrue= mysql_fetch_array($uses)))

{$email= $usertrue[email];


} else {die( "Wrong Password"); }
$updir = "/home/splinter239/public_html/images/recaps"; //absolute path to where files are uploaded, no trailing slash
$sizelim = "yes"; //do you want size limitations yes or no
$size = "505000"; //if you want size limited how many bytes
$certtype = "no"; //do you want certain type of file, no recommended
$type = ""; //what type of file would you like

//error if no file is selected
if ($file_name == "") {
echo("You didn't upload any images.<br>");
} else {
//error if file exists


//error if file isn't certain type

@copy($file, "$updir/$file_name") or die("The file you are trying to upload couldn't be copied to the server");
}
if (!$subject || !$article || !$username ||!$pass )
{
echo "You have not entered all the required details.<br>"
."Please go back and try again.";
exit;
}


$time= time();
if($newstype=="main") {
$query = "insert into news2 values ('$id','$time', '$subject', '$article', '$username','$email', '$file_name')";
$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()."
The news HTML has been built from the database. The latest news should now be visible on your page.
";
} else if ($newstype=="inter") {
$query = "insert into interviews values ('$id','$time', '$subject', '$article', '$username','$email', '$file_name')";
$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()."
Your Interview has been Posted you can see it now from the name page.
";

}


now i'm calling that file from testing.php

here is the code for testing.php

include "header.inc.php";

if($page=="createnews"){
if($news==ok)
{
postnews();
} else {
createnews();} } where i submit the form i get this error

You have not entered all the required details
Please go back and try again;

I have filled all the required details. If i put the put the code out of functions, it will work

spec
June 6th, 2002, 15:01
Ok i havent looked at it thuroughly but remember these simple rules of functions

when using alot of HTML simple brake out of PHP:



<?
// news form

function createnews() {
?>

<form enctype="multipart/form-data" action="" method=post>
<table width="31%" border="0">
<tr>
<td width="39%"><b>Username</b></td>
<td width="61%">
<input type="text" name="username">
</td>
</tr>
<tr>
<td width="39%"><b>Password</b></td>
<td width="61%">
<input type="password" name="pass">
</td>
</tr>
<tr>
<td width="16%"><b>Subject</b></td>
<td width="84%">
<input type="text" name="subject">
</td>
</tr>
<tr>
<td width="16%"><b>Where</b></td>
<td width="84%">
<select name="newstype">
<option value="main" selected>Main Page</option>
<option value="inter">Interviews</option>
</select>
</td>
</tr>
</table>
<p>&nbsp;</p><table width="75%" border="0">
<tr>
<td colspan="2">
<textarea name=\"article\" cols="60" rows="20"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="file" name="file" size="30">
<br>
<input type="hidden\" name="news" value="ok">
<input type="submit" name="Submit" value="Submit">
</div>
</td>
</tr>
</table></form>
<?


}


Secondly whenever you use variables from outside the function you must globalize them


<?
// insert news in the database
function postnews()
{
global $username, $pass, $etc;



Try that and it should work else come back for help

jetalomar
June 7th, 2002, 00:19
thanks dude