Jump to content

Error message


pwes24

Recommended Posts

Let's say i want convert km to miles. so the first thing is to check user input. if its not there an echo error like "enter some number" shows up. The problem is that this error shows up the first time the page loads. How can i prevent it from doing so?

Link to comment
Share on other sites

Let's say i want convert km to miles. so the first thing is to check user input. if its not there an echo error like "enter some number" shows up. The problem is that this error shows up the first time the page loads. How can i prevent it from doing so?

 

You need to do a CHECK first to see if they requested the data. I assume you have a submit button, we will call that "submit".

 

So you would just check if they pressed the button

 

<?php

if (isset($_POST['submit'])){
   //now put all the code here
}

?>

Link to comment
Share on other sites

You need to add some logic.

 

Assuming you are using a form to gather the data, at the top of the page you need to first check if the form has been submitted.

 

if (!$_POST['submit']) {

do your form stuff here

}

else {

do your php here

}

Link to comment
Share on other sites

 

What am I doing wrong here?

 

<body>

<?php

if (isset($_POST['submit'])){

$food=$_GET[food];

if (!empty($food)) {

echo "The foods selected are:<br />";

foreach($food as $foodstuff) {

echo $foodstuff.", ";

} else {

echo('check a box please');

}

}

}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<fieldset>

<label>Italian <input name="food[]" type="checkbox" value="Italian" checked /></label>

<label>Mexican <input name="food[]" type="checkbox" value="Mexican" /></label>

<label>Chenese <input name="food[]" type="checkbox" value="Chenese" /></label>

</fieldset>

<input type="submit" value="submit" name="submit">

</body>

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.