I'm using Get method to add data that has been entered by user to the event table in database
as you can see I'm using the get method to see if the user has posted and then run the code but the if statement does not run and I can't find out where is the problem.
Any help would be useful
Thank you very much
if(isset($_GET['add']))
{
$title=$_POST['txttitle'];
$detail=$_POST['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
this is the full Code event.php
<?php
include_once('db_connection.php');
$sqlinsert="";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function goLastMonth(month, year)
{
if(month==1)
{
--year
month= 13;
}
--month
var monthstring=""+month+"";
var monthlength= monthstring.length;
if(monthlength<= 1)
{
monthstring="0"+monthstring;
}
document.location.href="<?php $_SERVER['PHP_SELF'];?> ?month="+monthstring+"&year="+year;
}
function goNextMonth(month , year)
{
if(month==12)
{
++year
month= 0;
}
++month
var monthstring=""+month+"";
var monthlength= monthstring.length;
if(monthlength<= 1)
{
monthstring="0"+monthstring;
}
document.location.href="<?php $_SERVER['PHP_SELF'];?> ?month="+monthstring+"&year="+year;
}
</script>
</head>
<body>
<?php
if(isset($_GET['day']))
{
$day=$_GET['day'];
}
else
{
$day=date('j');
}
if(isset($_GET['month']))
{
$month=$_GET['month'];
}
else
{
$month=date("n");
}
if(isset($_GET['year']))
{
$year=$_GET['year'];
}
else
{
$year=date("Y");
}
$t=date('H:i');
//$day=date('j');
//$month=date("n");
//$year=date("Y");
$time=$day.",".$month.",".$year;
$currenttimestamp= strtotime("$year-$month-$day");
$monthname=date("F",$currenttimestamp );
$numdays= date("t",$currenttimestamp);
if(isset($_GET['add']))
{
$title=$_POST['txttitle'];
$detail=$_POST['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
?>
<?php echo $sqlinsert;?>
<table border="2">
<tr>
<td colspan="5">Month And Year <?php echo $monthname;?></td>
<td ><input style="width:55px" type="button" value="<<" name="previousbutton" onclick="goLastMonth(<?php echo $month. ",".$year;?>)"> </td>
<td> <input style="width:55px" type="button" value=">>" name="nextbutton" onclick="goNextMonth(<?php echo $month. ",".$year;?>)"> </td>
</tr>
<tr>
<td width="50">Sun</td>
<td width="50">Mon</td>
<td width="50">Tue</td>
<td width="50">Wed</td>
<td width="50">Thu</td>
<td width="50">Fri</td>
<td width="50">Sat</td>
</tr>
<?php
$counter="";
echo "<tr >";
for ($i=1;$i< $numdays+1; $i++, $counter++)
{
$timeStamp=strtotime("$year-$month-$i");
if($i==1)
{
$firstDay=date("w", $timeStamp);
for($j=0;$j<$firstDay; $j++, $counter++)
//blank space
{
echo "<td> </td>";
}
}
if($counter %7==0)
{
echo "</tr><tr>";
}
$monthstring=$month;
$monthlength= strlen ($monthstring);
//
$daystring=$i;
$daylength= strlen($daystring);
if($monthlength<=1)
{
$monthstring="0".$monthstring;
}
if($daylength<=1)
{
$daystring="0".$daystring;
}
echo "<td align='center'><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
}
echo"</tr>";
?>
</table>
<?php
if(isset($_GET['v']))
{
echo"<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true&f=true'>ADD EVENT</a>";
if(isset($_GET['f']))
{
include("eventform.php");
}
}
?>
</body>
</html>
and this is Event Form
<form name="eventform" name="POST" action="<?php $_SERVER['PHP_SELF'];?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year;?>&v=true&&add=true">
<table width="400px">
<tr>
<td width="150">Title</td>
<td width="250"><input type="text" name="txttitle"></td>
</tr>
<tr>
<td width="150">Detail</td>
<td width="250"><textarea name="txtdetail"></textarea></td>
</tr>
<tr>
<td colspan='2' align="center"><input type="submit" name"btnadd" value="Add Event"></td>
</tr>
</table>
</form>
PHP:
if(isset($_GET['btnadd']))
{
$title=$_GET['txttitle'];
$detail=$_GET['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
And HTML:
<form name="eventform" method="GET" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="400px">
<tr>
<td width="150">Title</td>
<td width="250"><input type="text" name="txttitle"></td>
</tr>
<tr>
<td width="150">Detail</td>
<td width="250"><textarea name="txtdetail"></textarea></td>
</tr>
<tr>
<td colspan='2' align="center"><input type="submit" name"btnadd" value="Add Event"></td>
</tr>
</table>
</form>
method="GET" automatically adds the vars to the URL, you do not need to add them in the action.
There are various errors in your code, but the main is that you are setting <form name="POST">
. I believe that you want to set <form method="POST">
.
first of all, if you go for method GET.. any user by just refreshing the page after sending, it would bucle your action, cause PHP will check for the url form if it has a GET method form.
also, your PHP code seems to be a bit wrong..
if(isset($_GET['add']))
{
$title=$_POST['txttitle'];
$detail=$_POST['txtdetail'];
$eventdate=$month."/".$day."/".$year;
$sqlinsert="INSERT INTO book (title, detail, eventdat, dateadded) VALUES ('{$title}', '{$detail}', '{$eventdate}',now())";
$result = mysql_query($sqlinsert, $connect);
if($result)
{
echo"date has been added";
}
else
{
echo "ops there was problem ";
}
}
your PHP is checking if theres a var, (array="add"), so its checking if there's an input with the name "add" in your form, i think you are trying check for "btnadd" because your submit button has that name.
and you have an error in your input:
<input type="submit" name"btnadd" value="Add Event">
you didnt enter "=", so should be:
<input type="submit" name="btnadd" value="Add Event">
where "btnadd" is has to be in your PHP like this
if(isset($_GET['btnadd']));//Exact var name