Solved

Php echo Alert with js

Posted on 2004-09-10
9
726 Views
Last Modified: 2013-12-12
In this script below can anyone show me how to write a alert box in js into the
echo statement  - echo 'that email address already exists!'; so it displays in a alert box





if($_POST['Submit'] == "Subscribe") {
    if($_POST['Mailname'] != "" && $_POST['Mailmail'] != "") {
        $mailAdd = mysql_query("INSERT INTO mailing VALUES('','".$_POST['Mailmail']."','".$_POST['Mailname']."')");
        if (!$result) {
            if (mysql_error() == 1062) {
                echo 'that email address already exists!';
            }
        }
    }
}
0
Comment
Question by:smphil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12026573
Try this...

if($_POST['Submit'] == "Subscribe") {
    if($_POST['Mailname'] != "" && $_POST['Mailmail'] != "") {
        $mailAdd = mysql_query("INSERT INTO mailing VALUES('','".$_POST['Mailmail']."','".$_POST['Mailname']."')");
        if (!$result) {
            if (mysql_error() == 1062) {
                echo '<script language="javascript">alert("That email was already entered.");</script>';
            }
        }
    }
}
0
 
LVL 1

Author Comment

by:smphil
ID: 12026929
Hey Pal
I closed out the other question and gave ya the points this is really a whole new question but you did a very nice job for me and I was able to understand and leard for your answers for future use.

______________________________________________________________________
Ok I tried the JS you did its not working I also found that the echo in plain old php is nt either. But here is the problem the script for the email subscribe is its own file called registermail.php now I have around 25 pages that have the small form to gather your email and name then send it to register.php then of course it goes to mysql. But the problem is we don't end up on register.php it only processes the info and the actual page you subscribed on stays in your window  <?php echo $PHP_SELF; ?> so I think thats why the JS nore the php echo don't work because we never pull the processing form into a window. how do I get the alert box to come up in the users window if the page the mail script is on(registermail.php)  neversee s the window ? I hope you understand my delemer.
Once agian thanks for the other script help Enjoy the points
Phil
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12027004
Ok.  So you're saying you have the following functionality?

1)  A user is on any random page.  The page has a small form to enter their email.
2) The user puts his email address in a textbox on the form, then clicks submit.
3) This form is submitted to registeremail.php.
4) Registeremail.php tries to add the email address to the database, and then returns the user to the page he/she was on.

That is what I understand is going on.

Why don't you, then, have the following code on your registermail.php:

if($_POST['Submit'] == "Subscribe") {
    if($_POST['Mailname'] != "" && $_POST['Mailmail'] != "") {
        $mailAdd = mysql_query("INSERT INTO mailing VALUES('','".$_POST['Mailmail']."','".$_POST['Mailname']."')");
        if (!$result) {
            if (mysql_error() == 1062) {
                // send user to error page...
                header("Location: email_exists.php");
            }
        } else {
            // send user back to the page he was on...
        }
    }
}

Where your email_exists.php page could just be a page (with the same layout as the rest of your site) that says "The email you entered already exists.  Please enter another."  And then it can display the form again.

Will this work for you?  This is how I do this kind of error processing on my own php sites...
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 1

Author Comment

by:smphil
ID: 12027422
Hey Pal let me show you mysite I am building and maybe you can show me the best way to display the message I have no room on the outsides or foorter headers and in the middle is content, let me know what you think. The site is a way from being complete but you will get the idea if you would like to see the search feature work the db is not populated so use these parameters
COUNTRY----US
State -use either  ---  FL  - NY
Sex  M or F
dont go any further because there are only a few entries for testing.
go to

http://mymodelfinder.com
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12027525
Hey...

I tried to add my email like 5 times.  It never let me know if it was added or not.

My suggestion:  when you do a search and get no results, the page displays:

No models with these Parameters where found... Please try again!

So, I suggest showing the same type of error when you try to add an email address that already exists.
0
 
LVL 1

Author Comment

by:smphil
ID: 12027720
so what you are saying is use the templete page I use to make all my other pages with the search feature and stuff and put the mailscript on it?
and call that the registermail.php  and take the <?php echo $PHP_SELF; ?> out of the form action on the small subscribe form on all the other pages. If I use this method would you please help just straightn out my add email script to have the echos for the string YOUR EMAIL HAS BEEN ADDED SUCCESSFULLY  OR YOUR EMAIL ALLREADY EXSITIST IN OUR DATABASE.
I think you are correct this is the best way

another possibilty-
But is there anway I can just have it call the alert box to the page and say wether they where added or not.


2 posibilty- I'd like to have this

Keep everything as I have on the regular pages. But when they emter there email when the backend script registermail.php processes the form and deterimines wether or not the email is allready in the db it spawns a small popup window saying one of the two echo strings.

0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 500 total points
ID: 12027814
Yes, I'm saying use the SAME template for your registermail.php page.

Try something like this:

{ registermail.php }

<?php
//put your php here that will try to add the user...
if($_POST['Submit'] == "Subscribe") {
    if($_POST['Mailname'] != "" && $_POST['Mailmail'] != "") {
        $mailAdd = mysql_query("INSERT INTO mailing VALUES('','".$_POST['Mailmail']."','".$_POST['Mailname']."')");
        if (!$result) {
            if (mysql_error() == 1062) {
                $msg = "That email address already exists in the database!";
            }
        } else {
            $msg = "Your email address has been successfully added!";
        }
    }
}

?>

<html>
<head>
<script language="javascript">
<!--
function displayAlert() {
    alert("<? echo $msg; ?>");
}
-->
</script>
</head>

<body onload="displayAlert();">
  PUT YOUR HTML STUFF HERE...
</body>
</html>
0
 
LVL 1

Author Comment

by:smphil
ID: 12029530
CFLVA
What I did is put this script on a new page and called that page registerMail.php
then I changes all the form actions to got to that page but why when there is a duplicate email won't it display the echo message? It does not let the email eb duplicated in the base but KNOW ECHO MESSAGE ON THE PAGE WHY.

the script on the page
<?php
        if($_POST['Submit'] == "Subscribe") {
    if($_POST['Mailname'] != "" && $_POST['Mailmail'] != "") {
        $mailAdd = mysql_query("INSERT INTO mailing VALUES('','".$_POST['Mailmail']."','".$_POST['Mailname']."')");
        if (!$result) {
            if (mysql_error() == 1062) {
                echo 'that email address already exists!';
            }
                  
        }
    }
}
?>
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12030188
Because your line of code should be:

 if (!$mailAdd) {

not
 if (!$result) {


:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question