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
https://forums.phpfreaks.com/topic/217855-getting-form-data/
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
https://forums.phpfreaks.com/topic/217855-getting-form-data/#findComment-1130710
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
https://forums.phpfreaks.com/topic/217855-getting-form-data/#findComment-1130725
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
https://forums.phpfreaks.com/topic/217855-getting-form-data/#findComment-1130731
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.