表单提交后,php变量未设置为所需的值

时间:2010-08-18 23:38:11

标签: php variables post

//get var of posted info so user does not 
//have to reinsert if if validation = false
if(!isset($_POST['FactionChanges'])){ $faction_name = ''; }
else { $faction_name = "".$_POST['factionname'].""; }
//without form submit, num_errors = 0
if(!isset($_POST['FactionChanges'])){ $num_errors = 0; }
//without form submit, success = 0
if(!isset($_POST['FactionChanges'])){ $success = 0; }


//error handling section [upon form submit]
if(isset($_POST['FactionChanges'])){

//deal with individual form section posts
//-->Faction Name
if(isset($_POST['factionname'])){

  //set var for error
  //array to use here
  $errors = array();

  $unsani_faction_name = $_POST['factionname'];
  $new_faction_name = str_replace(",", "", $unsani_faction_name);
  $faction_name = mysql_real_escape_string($new_faction_name);
  $faction_name = preg_replace('/\s\s+/', ' ', $faction_name);
  $faction_name = stripslashes($faction_name);//strips slashes from name
  //remove special chars except: "& $ £ ^ - ( )"
  $faction_name = preg_replace('/[^a-z0-9\s£&$\^\(\)-]/i', '', $faction_name);  
  $string_length = strlen($faction_name);
  if($string_length < 3 || $string_length > 20){ 
  $errors[] = 'Name: [between 3-20 characters]'; }
  else{ 
  $sql = mysql_query("SELECT * FROM ".TBL_TBL_FACTIONS." 
  WHERE f_name='$faction_name'"); 
  $num_rows = mysql_num_rows($sql);
  if ($num_rows > 0){ $errors[] = 'Name: [same faction name in existance]'; }
  else {  mysql_query("UPDATE ".TBL_FACTIONS." SET f_name='$faction_name' 
  WHERE f_id=$userfaction_id"); 
  header("Location: ".$page_name."?section=actions");
  //$success = 1;  [DOES NOT work here]
  }
}//else
}//if post factionname

$num_errors = count($errors);
if($num_errors === NULL){ $success = 1; }


}
//$success = 1;  [works here]



if($num_errors > 0){ echo '<p class="error"><strong>Error:</strong> 
The form could not be submitted because there are errors present</p>'; }

//add something here to only display sucess only if form is success
if($success == 1){ echo '<p class="success"><strong>Success:</strong> 
Your faction changes have been made</p>'; }

提交成功的表单输入时,我无法显示成功框? 我自己做了一点调试,并且插入了我尝试插入成功变量的地方。我真的很难过这个。

2 个答案:

答案 0 :(得分:2)

查看//$success = 1; [DOES NOT WORK HERE]

前面的行
 header("Location: ".$page_name."?section=actions");

您正在将用户重定向到另一个页面。当然,您的脚本将继续执行,但在您离开的页面上设置成功并不会做任何事情。

您的意思是将&success=1添加到重定向查询字符串吗?

答案 1 :(得分:0)

看起来问题出在以下几行:

$num_errors = count($errors);
if($num_errors === NULL){ $success = 1; }

由于count永远不会返回NULL,因此$ success将永远不会成立。如果您将其更改为:

$num_errors = count($errors);
if($num_errors == 0){ $success = 1; }

然后它应该工作。