Solved

Php echo Alert with js

Posted on 2004-09-10
9
710 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
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:smphil
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 13

Expert Comment

by:cLFlaVA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Because your line of code should be:

 if (!$mailAdd) {

not
 if (!$result) {


:)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now