有条件地设置表单提交动作

时间:2012-10-27 04:06:05

标签: php forms

<form action="<?php if(isset($redirect)&& $redirect == "f"){echo "#";} 
else{ ?>add_new_item2.php?user_id=<?php echo $_GET['user_id']; }?>" method="post"> 
//no need to expose this area here
<input type="submit" value="submit" name="submit" />
</form>

//the processor

<?php 
if(isset($_POST['submit']))
{
$redirect = "f";
    if(whatever)
    {
     //the if condition comes here
    }
    else //all if conditions are satisfied
    {
       $redirect = "t";
    }
}

我想做的事情很简单,在表单操作中我希望操作成为action =“#”如果if条件尚未满足,如果满足,则操作将是action =“add_new_item2.php?user_id = 123"

4 个答案:

答案 0 :(得分:1)

试试这个

<?php
$action = ''; 
if(isset($redirect) && $redirect == "f") {
  $action = "#";
} else {
  $action = "add_new_item2.php?user_id=".$_GET['user_id'];
}
?>
<form action="<?php echo $action; ?>" method="post"> 

答案 1 :(得分:1)

你在使用前做出条件意味着......

<?php
$action = ''; 
if(isset($redirect) && $redirect == "f") {
  $action = "#";
} else {
  $action = "add_new_item2.php?user_id=".$_GET['user_id'];
}
?>

<form action="<?php echo $action; ?>" method="post">

答案 2 :(得分:0)

如果已设置重定向值。使用$_SERVER['PHP_SELF']对当前页面执行操作。

检查引号$redirect == "f"。它应该是单引号或正确转义。

<form name="form1" method="post" 
      action="<?php 
             if(isset($redirect)&& $redirect == 'f'){echo $_SERVER['PHP_SELF'];} 
             else{ echo 'add_new_item2.php?user_id='.$_GET['user_id']; }
            ?>"
>

答案 3 :(得分:0)

尝试在双引号前后加单引号。与此类似:

<input type=label name=\"Description1\" value='". $T_Description. "' style=\"width:650px; margin-left:50px; padding: 5px;\" \>

(value ='“ x ”') 希望这会有所帮助...