开发者

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 factio开发者_开发技巧n 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]

 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?


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. If you change it to:

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

then it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜