dongshanyan0322 2014-04-17 12:37
浏览 160
已采纳

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

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

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>
  • 写回答

3条回答 默认 最新

  • drflkphi675447 2014-04-17 12:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度