Jump to content

Logical Sequence Mistake or Variable Assignment Mistake


cosmic_sniper

Recommended Posts

Hi there,

 

First of all, I want to present the diagram that I'm working on.

-----------

Diagram

-----------

step2_a.php --|

step2_b.php --|---> output.php

step2_c.php --|      (Process is included here)

step2_d.php --|

 

 

and here are the corresponding codes:

 

step2_x:

. . .
<form action="output.php" method="post" name="a">
req1: <input type="text" name="a_req_1">
req2: <input type="text" name="a_req_2">
. . .
<input type="submit">
</form>
------
. . .
<form action="output.php" method="post" name="b">
req1: <input type="text" name="b_req_1">
req2: <input type="text" name="b_req_2">
. . .
<input type="submit">
</form>
. . .
. . .

 

output.php:

<?php
$form_name = $_POST['a'];
if($form_name = "a") {
//variables for a goes here
        //code goes here
}
elseif($form_name = "b") {
//variables for b goes here
        //code goes here
}
elseif($form_name = "c") {
//cariables for c goes here
        //code goes here
}
elseif($form_name = "d") {
//variables for d goes here
        //code goes here
}
else {
//code goes here
}
?>

Now, here's the problem. output.php always sees the primary condition as true. Even the data came from step2_b, the code does not recognize it. I tried to use "==" instead of "=" but the code sees the arguments as false so it executed the code on "else".

 

How can I make the code distinguish the source of the data so that it would execute the right set of commands?

 

Thanks in advance.

Link to comment
Share on other sites

Don't just slap things into the code hoping it will work. Make sure you understand what you are doing. using a single equal sign is an assignment operator. It assigns the second argument to the first argument. So, if you have

if($foo = 'bar')

That will always return true because the PHP parser can successfully assign the value of 'bar' to the variable $foo.

 

However,a double equal sign is a comparison operator - which is what you would normally use in an if() statement. So, if this

if($form_name == "a") {

is always returning false, that is because $form_name does not equal the string 'a'.

 

You are assigning the value to $form_name using

$form_name = $_POST['a'];

but there is no input field with the name of 'a', so I would assume $form_name is always equal to NULL. I see fields called 'a_req_1' and 'a_req_2' though.

 

I really can't provide any specific code help because I have no idea what fields you should be using or what your ultimate goal is.

Link to comment
Share on other sites

What I'm actually trying to do in the code is first identify the source of data. So since there are four different files pointing towards output.php and the data would all be coming from a form, i decided to name each form as a, b, c, d respectively.

 

That is why I assigned $form_name = $_POST['a'] (the name of form from step2_a.php) then I intend to set if() statement to validate if the request really came from step2_a.php. If not, then it might be from other forms and that's where elseif() comes in.

 

I tried to use "is identical" to replace "is equal to" to check further check the interpretation of the code. Then after execution, the if)_ statements turned to be false (most probably due to the reason you have explained). Comparing it with the initial "is equal to" assignment, where it does not return false on every if() argument. From there, I've learned that the code does not actually identify it that way. (mostly a self-help to understand how it language runs)

 

Anyway, the bottom line is, I want the code to identify the source of the data and execute the corresponding set of commands.

 

I hope this helps to explain how I intend it to be structured.

Link to comment
Share on other sites

<form action="output.php" method="post" name="a">
req1: <input type="text" name="a_req_1">
req2: <input type="text" name="a_req_2">
. . .
<input type="submit">
</form>

 

$form_name = $_POST['a'];
if($form_name = "a") {
//variables for a goes here

 

The name attribute of the FORM tag is not posted with the form. I have always thought this is a slight failing in the specifications, but they didn't ask me.  ;)

 

To determine which form was posted. Add a hidden field to each form using the same name but with different values:

 

<form action="output.php" method="post" name="a">
req1: <input type="text" name="a_req_1">
req2: <input type="text" name="a_req_2">
<INPUT type="hidden" name="whichForm" value = "a">
. . .
<input type="submit">
</form>

Link to comment
Share on other sites

EDIT: DavidAM already provided the same solution, but I'll leave this anyway since I included the use of switch() which is more appropriate for this.

 

That is why I assigned $form_name = $_POST['a'] (the name of form from step2_a.php) then I intend to set if() statement to validate if the request really came from step2_a.php. If not, then it might be from other forms and that's where elseif() comes in.

 

. . .

 

Anyway, the bottom line is, I want the code to identify the source of the data and execute the corresponding set of commands.

 

The form name is not included in the POST data. Only the fields are passed. What you will want to do is create a hidden field in each form to determine which processing to do. And, if the input fields for each form is the same, then make the field names the same.

 

Form

<form action="output.php" method="post" name="a">
req1: <input type="text" name="req_1">
req2: <input type="text" name="req_2">
<input type="hidden" name="form" value="a">
. . .
<input type="submit">
</form>
------
. . .
<form action="output.php" method="post" name="b">
req1: <input type="text" name="req_1">
req2: <input type="text" name="req_2">
<input type="hidden" name="form" value="b">
. . .
<input type="submit">
</form>

 

Processing code

switch($_POST['form'])
{
    case 'a':
        //Insert processing logic for form a here
        break;

    case 'b':
        //Insert processing logic for form b here
        break;

    case 'c':
        //Insert processing logic for form c here
        break;

    case 'd':
        //Insert processing logic for form d here
        break;
}

Link to comment
Share on other sites

Thanks for the help!  ;)

 

That finally made my day. I should have known it all along that the form name is not posted along with the content of the form. That hidden input is the "missing variable" that I've been looking for. Thanks for bringing that up.

 

I finally get things to work the way I want it.

 

But just in case another newbie would be reading this thread, I would like to bring up one significant factor that made it work.

 

After adding the <input type="hidden" name="consistent_name_across_sources" value="varrying_value_across_sources">, it is necessary to use == instead of = in the if() and elseif() statements. That made it work for me.

 

I have an idea on why it is so but I believe that other guys here are in better position to explain it. After all, mine was just an educated guess and not substantially acceptable fact.

 

Thanks again ^_^

Link to comment
Share on other sites

After adding the <input type="hidden" name="consistent_name_across_sources" value="varrying_value_across_sources">, it is necessary to use == instead of = in the if() and elseif() statements. That made it work for me.

 

I have an idea on why it is so but I believe that other guys here are in better position to explain it.

 

I DID cover that in my first response, did I not? Even so, as I stated in my previous post a switch() statement is a more appropriate solution than a bunch of if/else statements in this scenario.

Link to comment
Share on other sites

Oh yah Psycho. You did bring that up already.

which is what you would normally use in an if() statement

Sorry to miss that specific point. My thoughts were kinda diverted on explaining my the structure that I want.

 

Regarding the use of swithc() statement, thanks for mentioning that. It's just that I have fixed the codes before I read your response but I totally believe in what you said that switch() statement is more appropriate for this. I would be revising my code using that.

 

Thanks.

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.