The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 3 of 3

Thread: echo strings in an array to a javascript alert

  1. #1

    echo strings in an array to a javascript alert

    I have a form that posts data to itself. If there are any errors I want it to alert the user via javascript. So far the alert box only shows "Error: " (It also shows the correct number of errors, I think)which means there is just a problem in getting the array to echo correctly.

    Code:
    <?php
    function login($subemail, $subpass, $subremember) {
      $err = array();
      if (!$subemail || strlen($subemail = trim($subemail)) == 0) {
        array_push($err, "Email field is blank, please enter your email.");
      }
      if (!$subpass) {
        array_push($err, "Password field is blank, please enter your password.");
      }
      $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"."\.([a-z]{2,}){1}$";
      if(!eregi($regex,$subemail)){
        array_push($err, "Email is not valid, please enter you email.");
      }
      if(count($err) != 0) {
        return $err;
      }
      
      $link = mysql_connect($dbserv, $dbuser, $dbpass);
      mysql_select_db($dbname, $link);
      $q = "SELECT password FROM ".TBL_USERS." WHERE username = '$subemail'";
      $result = mysql_query($q, $link);
      if(!$result || (mysql_numrows($result) < 1)){
        array_push($err, "Email is not registered. Please register this email or enter a registered email.");
        return $err;
      }
      $dbarray = mysql_fetch_array($result);
      if($subpass != $dbarray['password']){
        array_push($err, "Incorrect Password.");
      }
      if ($subremember) {
        setcookie("email", $subemail, time()+60*60*24*100);
        setcookie("userid", makeUserID(), time()+60*60*24*100);
      }
      return $err;
    }
    
    if (isset($_POST['subemail']) || isset($_POST['subpass']) || isset($_POST['remember'])) {
      $loginerrors = login($_POST['subemail'], $_POST['subpass'], isset($_POST['remember']));  
      if (count($loginerrors) != 0) {
        $alert = "";
        for($i=0; $i<count($loginerrors); $i++) {
          $alert .= "Error: {$loginerrors[i]}";
          if (i != count($loginerrors)-1) {
            $alert .= "\\n";
          }
        }
      }
    }
    
    ?>
    <!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" />
    <?php
    if ($alert) {
    ?>
    <script type="text/javascript">
    <!--
    
    window.alert ("<?php echo $alert; ?>");
    
    // -->
    </script>
    <?php
    }
    ?>

  2. #2
    change this line:

    PHP Code:
    $alert .= "Error: {$loginerrors[i]}"
    to this:

    PHP Code:
    $alert .= "Error: " $loginerrors[$i]; 
    Blog: http://www.gregathons.com

    Why do web browsers function so differently? Why can't they all follow the same Web Standard...users don't care how it works, but the programmers do.

  3. #3
    Rats, I knew it was just something stupid that I overlooked. I had tried that just without the $ in front of i. Too used to Actionscript, Too new to PHP.

    Thanks.
    Last edited by mattrock23; January 25th, 2008 at 07:01 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012