简体   繁体   English

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

[英]php variable is not setting to desired value after form submission

//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>'; }

I can't get the success box to display when a successful form input is submitted? 提交成功的表单输入后,我无法显示成功框? I've done a little debugging myself, and I've inserted where I've tried inserting the success variable. 我做了一些调试工作,然后将其插入尝试插入成功变量的位置。 I'm really quite stumped on this one. 我真的很为难。

Look at the line immediately preceding your //$success = 1; [DOES NOT WORK HERE] 看一下//$success = 1; [DOES NOT WORK HERE]之前的那一行//$success = 1; [DOES NOT WORK HERE] //$success = 1; [DOES NOT WORK HERE]

 header("Location: ".$page_name."?section=actions");

You're redirecting the user to another page. 您正在将用户重定向到另一个页面。 Sure, your script will keep executing, but setting success on a page you are leaving doesn't do anything. 当然,您的脚本将继续执行,但是在要离开的页面上设置成功不会执行任何操作。

Do you mean to add &success=1 to your redirect query string? 您是否要在重定向查询字符串中添加&success=1

it looks like the problem is with these lines: 这些行似乎是问题所在:

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

Since count never returns NULL, $success will never be true. 由于count永远不会返回NULL,因此$ success永远不会为true。 If you change it to: 如果将其更改为:

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

then it should work. 那它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM