Jump to content

Recommended Posts

Hello, i'm just starting out with PHP and have LOTS to learn. It can be frustrating at times but yet very addictive :P. Anyways i've just been playing around with it and i'm having a problem with this code:

 

<?php

 

$user = $_POST['user'];

$name = $_POST['name'];

$sub = $_POST['submit'];

 

if(isset($sub)){

if(!isset($user) || !isset($name)){

echo "Failed";

}else{

echo "Complete";

}

}

 

?>

 

The problem is that when clicking the "submit" button on my form nothing is being echo'd (Failed or Complete). I have tried filling the fields and leaving the fields empty but still nothing being echo'd  ::). Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/
Share on other sites

your variables are always set because you set them with "="

 

use something like

 

if($_POST[submit]){

$user= $_POST[user];

$name = $_POST[name];

 

if(strlen($user) < 1 || strlen($name) < 1){

echo "failed";

}else{

echo "completed";

}

 

 

Try this:

 

<?php

$user = $_POST['user'];
$name = $_POST['name'];

if($_POST['submit']){

   if ((!isset($user)) || (!isset($name)){
   echo "Failed";

   }else{

   echo "Complete";
   }
}
   
?>

 

Are you sure you have your form pointing to the right script?

Thanks for the help guys, although im still getting no results after trying the suggestions. I'm 99% sure my form is setup right, but I am a little tired lol so here's my form maybe you guys can catch something i'm not seeing.

 

<form method="post" action="register.php">

<b>Username:</b><br>

<input type="text" name="user"><br>

<b>Name:</b><br>

<input type="text" name="name"><br>

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

</form>

 

The PHP file this form is calling is indeed named register.php.

Just to correct my code earlier:

 

<?php

if($_POST['submit']){

$user = $_POST['user'];
$name = $_POST['name'];

   if ((!isset($user)) || (!isset($name)){
   echo "Failed";

   }else{
  
   echo "Complete";
   }
}
   
?>

That code will not work, specifically this:

$user = $_POST['user'];
$name = $_POST['name'];

   if ((!isset($user)) || (!isset($name)){

That if statement will always return false, meaning it will allways execute the else part of the if statement, as you have created the $name and $user variables in your code. It should be like this:

   if ((!isset($_POST['user'])) || (!isset($_POST['name'])){

 

This code is better:

if(isset($_POST['submit']))
{
    if ((!isset($_POST['user'])) || (!isset($_POST['name']))
    {
        echo "PLease encsure all form fields are filled in";
    }
    else
    {
        echo "All form fields have been filled in!";

        $user = $_POST['user'];
        $name = $_POST['name'];
    }
}

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.