Link to home
Start Free TrialLog in
Avatar of smphil
smphilFlag for Afghanistan

asked on

Php echo Alert with js

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!';
            }
        }
    }
}
Avatar of cLFlaVA
cLFlaVA

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>';
            }
        }
    }
}
Avatar of smphil

ASKER

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
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...
Avatar of smphil

ASKER

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
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.
Avatar of smphil

ASKER

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.

ASKER CERTIFIED SOLUTION
Avatar of cLFlaVA
cLFlaVA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smphil

ASKER

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!';
            }
                  
        }
    }
}
?>
Because your line of code should be:

 if (!$mailAdd) {

not
 if (!$result) {


:)