php - If first $_GET field is empty but second has a value, echo to fill in missing value -
i want "please fill in form below:" @ first, before input, afterwards if input age no name, want say: "please enter name:" , if input name no age then: "please enter age:" if submit nothing then: "error: please enter name , age continue"
here code have:
if( $_get["name"] && $_get["age"] ) { echo "<p>welcome ". $_get['name']. " ... "; echo "you ". $_get['age']. " years old.</p>"; exit(); } elseif ( $_get["name"] == false ) { echo "<p>please fill in form below:</p>"; } elseif ( $_get["age"] == false ) { echo "<p>please fill in form below:</p>"; } elseif ( $_get["name"] == false && $_get["age"] ) { echo "<p>please fill in name:</p>"; } elseif ( $_get["age"] == false && $_get["name"] ) { echo "<p>please fill in age:</p>"; }
i'm learning php , i've tried can think of, i've ended far!
thank you.
replace false
empty()
, you're there
your form:
<form method='post' ... > <input type='text' name='name' value=''> ... </form>
then php:
if( $_post["name"] ... ) { ... exit; } elseif ( empty($_post["name"]) ) { echo "<p>please fill in form below:</p>"; } elseif ( empty($_post['age'] || !is_numeric($_post["age"]) ) { echo "<p>please fill in form below:</p>"; } elseif ( empty($_post['name'] && !empty($_post['age'] ) { echo "<p>please fill in name:</p>"; } elseif ( !empty($_post['name'] && empty($_post['age']) { echo "<p>please fill in age:</p>"; }
you should restructure logic 2 values independent
- check if name filled in.
- check if age filled in.
- return echo statement user.
so above series of elseif
statements can become...
if(empty($_post['name'])){ $notice .= " please enter name."; } if(empty($_post['age'])){ $notice .= " please enter age." } print $notice.
the .=
concatenates new definition on end of current (or empty) value of variable. can print $notice
every time because either empty (nothing printed) or has value, want print, it's win win every time.
Comments
Post a Comment