[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

php empty field

one of the fields lastname, has a value

so if screen is refreshed    (f5)
then firstname is empty and lastname is has the value $fidnumber


want to make sure firstname is not empty
if (!empty($_POST["firstname"]) && !empty($_POST["lastname"])) 
{ 
    $fn  = $_POST["firstname"]; 
    $ln  = $_POST["lastname"]; 
            $sql = "INSERT INTO PACKAGES (TITLE,FID,ACTIVE) VALUES ('$fn','$ln','1')";
        dbquery($sql); 
    echo "ADDED $fn $ln TO packages";  
}  

echo' 
<form method="post">  
type package title
<input name="firstname" />  
<br>type package folder id
<input name="lastname" name="lastname" value="'.$fidnumber.'"/><br>  
<input type="submit" value="Click here to save" />  
</form>
';     
 
echo '<br>';

Open in new window

0
rgb192
Asked:
rgb192
1 Solution
 
DrDamnitCommented:
In order to keep a variable from one page to the next, you need to use a session variable. Please try the code below.

Remember, session_start() should be the very first line of the page or you'll get errors.
session_start();
if (!empty($_POST["firstname"]) && !empty($_POST["lastname"]))
{
    $fn  = $_POST["firstname"];
    $ln  = $_POST["lastname"];
            $sql = "INSERT INTO PACKAGES (TITLE,FID,ACTIVE) VALUES ('$fn','$ln','1')";
        dbquery($sql);
    echo "ADDED $fn $ln TO packages";
    $_SESSION['fidnumber'] = $fn;
}

?>
<form method="post">
type package title
<input name="firstname" />
<br>type package folder id
<input name="lastname" name="lastname" value="<?php if(isset($_SESSION['fidnumber']) echo $_SESSION['fidnumber']; ?> "/><br>
<input type="submit" value="Click here to save" />
</form>

<br>
<?php
//...continue your code here.

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello rgb192,

With Javascript :


<form method="post">  
type package title
<input name="firstname" id="lastname" onkeyup="(this.value.length>0)?document.getElementById('subbut').disabled=false:document.getElementById('subbut').disabled=true;" />  
<br>type package folder id
<input name="lastname" id="lastname" value="'.$fidnumber.'"/><br>  
<input type="submit" id="subbut" value="Click here to save" disabled />  
</form>

Open in new window

0
 
Ray PaseurCommented:
@rgb192, Remember when we pleaded with you to NOT SUPPRESS warning and notice messages, and instead fix the problem that was causing the messages?  Well, this is what we were talking about.  In your posted script, you make reference to a variable called $fidnumber.  There is no definition for that variable in the code you posted.  So either the variable is undefined or you are hiding the code from us that creates the variable.  We are not very good at guessing what you might be doing behind the curtain - just experts, not clairvoyants.  If you are using error_reporting(E_ALL) as has been suggested you would know where this variable gets its value - if it gets a value.

In addition to that your code snippet takes unfiltered data from the post string and uses it in a data base query.  Doing that is one way to be certain that catastrophe is not left to chance.

To this part of the question, that I can readily understand and help with:

"want to make sure firstname is not empty"

You can use the empty() function.  Simply test the field to see if it is empty!  You did not tell us what you want to make happen if the field is empty, but if you test it and find that it is empty, then you can have program logic to deal with that.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now