0

I have a simple form where a user adds a type of act. Then saves it.

They can then go back and edit it. When they go back to edit I want the type of act they are editing to show as selected in a drop down in the form.

I use a function to print the drop down box and am trying to send the current variable into the function to check and amend the results. Hope that makes sense!

The problem is that the variable $row[act_type] is NOT being seen inside the function

Anyway, here's how I call the function ($row[act_type] DOES exist):

edit_act_types('<?=$row[act_type]?>');

Then to print the drop down I'm using:

function edit_act_types($actType) {
    $mysqli = dbConnect(); // connect with db
    $query_actTypes="SELECT * FROM actTypes order by actType";
    $result_actTypes = $mysqli->query($query_actTypes);

    echo '<select class="form-control" id="type" name="type"><option value="">'.$actType.' Select...</option>';

    while($row_actType = $result_actTypes->fetch_array()){

        if($actType==$row_actType['actTypes_id']) {

            $selected = 'selected="selected"';

        }

        echo '<option value="'.$row_actType['actTypes_id'].'" '.$selected.'>'.$row_actType['actType'].'</option>';

    } 

    echo '</select>';
};
2
  • Why are you pasting PHP tags inside PHP? edit_act_types($row['act_type']);? (Also note the quote symbols unless you've declared it as a constant (which you probably haven't unless you have define('act_type', 'value') in your code)). Commented Oct 28, 2013 at 12:05
  • Shouldn't your edit_act_types('<?=$row[act_type]?>'); function call be simply edit_act_types($row[act_type]);? Commented Oct 28, 2013 at 12:07

2 Answers 2

1

The function is called in php tag

<?php   
 edit_act_types($row[act_type]);
?>

So <?=$row[act_type]?> -- will not work

Sign up to request clarification or add additional context in comments.

Comments

1

You have the following line in your code:

edit_act_types('<?=$row[act_type]?>');

I'm not sure what you're trying to achieve with the above line. The <?=$row[act_type]?> part will just be considered as a string and be passed as your function parameter instead of the actual variable. You don't need to use PHP tags when calling the function.

Consider the following piece of code:

header('Content-Type: text/plain');

function foo($bar) {
    echo $bar;
}

$var = 'hello';
foo('<?=$var?>');

The output will be <?=$var?>, not hello.

So, your function call should look like:

edit_act_types($row['act_type']);

Read more about Functions in the PHP Manual.

Comments

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.