0

php code

if(isset($_POST['txtLocation']))
{
    $choice_loc = $_POST["txtLocation"];
}
elseif(!isset($_POST['txtLocation']))
{
    $message = "Please select the desired location or click on default";
}
elseif($choice_loc == "txtSetXY")
{
    $x = $_POST["txtXLocation"];
    $y = $_POST["txtYLocation"];
    if($x == "")
    {
        $message = "You forget to enter X location.";
    }
    elseif($y == "")
    {
        $message = "You forget to enter Y location.";
    }
    else
    {
        $choice_loc = $x . "," . $y;
    }
}

This is html form

<div class="formText">
  <input type="radio" name="txtLocation" value="txtSetXY"/> Specify Location<br />
  <div style="padding-left:20px;">
       X: <input type="text" id="locField" name="txtXLocation">
       Y: <input type="text" id="locField" name="txtYLocation">
   </div>
   <input type="radio" name="txtLocation" value="Default" checked="checked"/>Default
</div>

What is the error in logic??

value "default" is entered into database, but when selected value="txtSetXY" radio and entering x and y values in textfields it is not entering into database?

this is my database entering query

$insert = "INSERT INTO dbform (dblocation) VALUES ('{$choice_loc}')";
4
  • It stopped at the second if statement. Commented Jan 31, 2013 at 17:14
  • @user543732 can we use any sort of php development platform where we can check our coding or dry run it like we do in c and C++ in turboC compiler? So that it may give the outputs and errors easily?? Commented Jan 31, 2013 at 17:18
  • There're a few, but I don't know if any of them can catch this kind of error. if A - do A. elseif not A - do B. It stopped right there, will never reach every elseif and else below it. It's as you said a logic error, not PHP syntaxes. Commented Jan 31, 2013 at 17:21
  • you can find an IDE that will help (PHPStorm*, Netbeans, Eclipse, etc). Also, run php file.php will run the file and give an output or even just browse to the page. To debug, put echo/print lines to determine where your script is going. Commented Jan 31, 2013 at 17:23

2 Answers 2

2

There is no way your test can enter the third choice :

elseif($choice_loc == "txtSetXY")

Because

if(isset($_POST['txtLocation']))
{
...
}
elseif(!isset($_POST['txtLocation']))
{
...
}

covers all the possible paths and could be replaced by

if(isset($_POST['txtLocation']))
{
...
}
else
{
...
}

where you would have seen that you couldn't add another test case.

Maybe you should try to invert the order in your test :

if(isset($_POST['txtLocation']))
{
    $choice_loc = $_POST["txtLocation"];
}
elseif($choice_loc == "txtSetXY")
{
    $x = $_POST["txtXLocation"];
    $y = $_POST["txtYLocation"];
    if($x == "")
    {
        $message = "You forget to enter X location.";
    }
    elseif($y == "")
    {
        $message = "You forget to enter Y location.";
    }
    else
    {
        $choice_loc = $x . "," . $y;
    }
}
else
{
    $message = "Please select the desired location or click on default";
}
Sign up to request clarification or add additional context in comments.

4 Comments

that means.. 3rd choice elseif($choice_loc == "txtSetXY") should be inside first choice?? if(isset($_POST['txtLocation']))
@Sumit No, it's between the if(isset(...)) and the else. This way, if the location isset, it takes the default location, if not, it checks if the coordinates were entered, and if none of these options are taken, it raises the error message. So at the end of the test, you can have $choice_loc filled with txtLocation, or x and y concatenation, else you get an error message.
actually the code i entered is not complete.. i.e. it does not only contains this if-else.. it is a series of if else and it checks many other fields so i dont think this blunt else else { $message = "Please select the desired location or click on default"; } will help..
can't help you further without more details on what you want to do, or what the context is.
0

You went a little overboard with your elseif's in the first logic section. You should try the following:

if(!empty($_POST['txtLocation']))
{
    $choice_loc = $_POST["txtLocation"];
}
else
{
    $message = "Please select the desired location or click on default";
}
if(isset($choice_loc) && $choice_loc == "txtSetXY")
{
    if(!empty($_POST["txtYLocation"]))
        $y = $_POST["txtYLocation"];
    else
        $message = "You forget to enter Y location.";

    if(!empty($_POST["txtXLocation"]))
        $x = $_POST["txtXLocation"];
    else
        $message = "You forget to enter X location.";

    if(isset($x) && isset($y))
    {
        $choice_loc = $x . "," . $y;
    }
}

4 Comments

ur code is putting values to database.. the concataneted one also.. but not doing empty x y validation
i used this code but again.. it is putting x and y to db.. but not checking if i leave them vacant
as an update.. this new code is now not even checking radio buttons.. i.e. it was worse than before..
what do you mean? Due to the default radio being checked, isset($_POST['txtLocation']) will always be true, therefore $choice_loc will always be set. What are you trying to check?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.