Alert box on PHP if name==NULL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Alert box on PHP if name==NULL

Post by ShadowSkill »

If the user inputs a blank/null value at $name
then id like to make a alert box that says please enter the missing values

ex:

if ($name==' ' or $address==' ' or $age==' ')
{
// Alert Box saying "Please enter the missing values"
}

Thank you to those who will help...
Love this helpful forum <3
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Alert box on PHP if name==NULL

Post by califdon »

Remember, PHP is a server side language and only works in the server, not the browser, so it can't possibly detect anything that the user does. You have to do this in Javascript. Rephrase your question and post in the Javascript forum here.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Alert box on PHP if name==NULL

Post by Celauran »

Since this looks like form validation, you can use PHP to do it. Place your JS alert code inside the PHP if condition.

Code: Select all

if (empty($name) || empty($address))
{
    echo "<script type=\"text/javascript\">alert('All fields must be completed');</script>";
}
or something to that effect.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Alert box on PHP if name==NULL

Post by califdon »

@ShadowSkill: Be sure you understand what Celauran is saying. He described a way to test for a blank required field after the form has been submitted to the PHP script that will process the data, to send a new page back to the browser, in which the pop-up will appear, but unless you include substantial additional code, all the previous form data will be reset, which is not very user-friendly. It is still Javascript that creates the alert pop-up. Usually you would want to check for blank required fields before submitting the form, since you don't want to process the data until the omission has been corrected. To do that, you would use Javascript, and thus this thread should be moved to our Javascript forum.
ShadowSkill
Forum Newbie
Posts: 22
Joined: Sun Jan 01, 2012 5:45 am

Re: Alert box on PHP if name==NULL

Post by ShadowSkill »

Thanks will try to read more and understand more about Java...
I'm sorry about this, its just that i'm studying this where my professors doesn't even know PHP...
They only know HTML...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Alert box on PHP if name==NULL

Post by califdon »

ShadowSkill wrote:Thanks will try to read more and understand more about Java...
I'm sorry about this, its just that i'm studying this where my professors doesn't even know PHP...
They only know HTML...
That's a bummer. I know it happens (I used to teach database and Internet courses in a community college), but it shouldn't.

Here are a few online tutorials that might help you:
http://www.maconstateit.net/tutorials/
http://www.w3schools.com/
http://www.tizag.com/

My recommendation is that you first learn HTML, then CSS, then Javascript (the 3 languages that are used by a browser), then PHP (the web server language).

Note that Javascript is NOT Java, not even close. They are 2 utterly different programming languages. Why did they name it Javascript? Everybody asks and there isn't a very satisfactory answer. Technically, its official name is ECMAScript, but nobody likes that, either.
Last edited by califdon on Sun Jan 22, 2012 10:22 pm, edited 1 time in total.
Reason: Added some references
Post Reply