Jump to content

Getting Form data


WideBlade

Recommended Posts

i'm trying to access the data here:

 

file.html

 

<html>
<head>
    <title>Online Marketing Calculators</title>
</head>

<body>
    <center>
        <h1>Online Marketing Calculators</h1>
        <h4> ROI Caclulator</h4>
        <form action="calculators.php" method="post">
            <p>Total Cost : <input type="text" name="cost">
            Total Income: <input type="text" name="income"></p>
            <input type="submit" value="submit">
        </form>
    </center>
</body>
</html>

 

 

 

file.php

 

<?php

if(isset($_REQUEST['cost']))
{
    echo $_REQUEST['cost'];
} else
{
    echo "Please Enter Cost";
}


?>

 

And it's not working. Any Ideas?

 

Link to comment
Share on other sites

<?php

if(isset($_POST['cost']) && !empty($_POST['cost'])){
    echo $_POST['cost'];
} else{
    echo "Please Enter Cost";
    exit;
}
?>

 

Try that, don't use $_REQUEST all sorts of issues there. And ensure that the file name you refer to in the action matches the file your editing, and that it is in the same directory.

 

Rw

Link to comment
Share on other sites

not set is not the same as empty. if you check to see if the variable is empty and it is not set, you'll get a warning. therefore, it is good practice to make sure a variable is set before doing any other comparison or operation on that variable:

 

if (isset($_POST['somevariable']) && $_POST['somevariable'] == 123) {
      // do something
}


Link to comment
Share on other sites

Thanks a lot RW!

 

Works like a charm One quesiton though: why do i have to check if ti is set and if it is empty?

Shouldn't one of them be enough to check if the user submitted something or not?

 

Right, glad that got you started. Now you need to 'catch' the form being submitted the correct way:-

 

<?php
//check that the form has been submitted via the submit button
if(isset($_POST['submit']) && !empty($_POST['submit'])){
//success, now you can clean and assign the $_POST data for use in your script
    echo $_POST['cost'];
} else{
//this is the error handler, usually best to have a header() call so that you can redirect back to the form
    echo "Please Enter Cost";
    exit;
}
?>

 

But yes, check that the var exists/has state, then check to see if contains anything, you can go more in depth than that, but for this excerpt, this will do the task.

 

Rw

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.