PHP - 使用Get方法提交表单。

时间:2014-04-17 12:37:19

标签: javascript php html

我使用Get方法将用户输入的数据添加到数据库中的事件表中

您可以看到我使用get方法查看用户是否已发布然后运行代码 但if语句没有运行,我无法找出问题所在。

任何帮助都会有用

非常感谢

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 ";
    }
}

这是完整的代码 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>&nbsp;</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>

这是事件表格

<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>

3 个答案:

答案 0 :(得分:1)

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 ";
    }
}

和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”会自动将变量添加到网址中,您无需在操作中添加变量。

答案 1 :(得分:0)

您的代码中存在各种错误,但主要是您正在设置<form name="POST">。我相信您要设置<form method="POST">

答案 2 :(得分:0)

首先,如果你去方法GET ..任何用户只需在发送后刷新页面,它就会破坏你的动作,因为如果它有一个GET方法表单,PHP会检查url表单。

另外,你的PHP代码似乎有点不对..

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正在检查是否有一个var,(array =“add”),所以检查你的表单中是否有一个名为“add”的输入,我想你正在尝试检查“btnadd”,因为你的提交按钮有这个名字。

您输入中有错误:

<input type="submit" name"btnadd" value="Add Event">

你没有输入“=”,所以应该是:

<input type="submit" name="btnadd" value="Add Event">

其中“btnadd”必须在你的PHP中

if(isset($_GET['btnadd']));//Exact var name