Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


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.

    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}$";
        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">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    if ($alert) {
    <script type="text/javascript">
    window.alert ("<?php echo $alert; ?>");
    // -->

  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.

    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


Copyright 1999 - 2012