0

I have a problem with my PHP variable and a button which is generated using 'echo'. Now I want to insert the id of the button. How I will do that.
Please help me. Code is given below.

if ($unconfirmed_cities) {
    foreach ($unconfirmed_cities as $unconfirmed_city) {
        echo '<div>';
        echo "$unconfirmed_city[name]";
        echo '<input type="button" id= "$unconfirm_city[name]" value="Accept" class="mybut btn btn-info btn-mini" style="">';
        echo '</div>';  
    }
}  

Now as given the input type = "button"in the echo tag and I have inserted the id but it is not working.How to do this.

3
  • 2
    I think that is something wrong with [name] - shouldn't it be ["name"] ? Commented Nov 1, 2013 at 12:00
  • @radeczek No, without any quotes it's correct too. Commented Nov 1, 2013 at 12:29
  • But it was not working in my code that's why I told you. Commented Nov 1, 2013 at 14:21

5 Answers 5

2

Try like

 echo '<input type="button" id= "'.$unconfirm_city['name'].'" value="Accept" class="mybut btn btn-info btn-mini" style="">';
Sign up to request clarification or add additional context in comments.

1 Comment

You missed the closing single quote.By the way thank you for your answer.
0

You have a lot of typo on your code. Try this way

<?php
if ($unconfirmed_cities) {
foreach ($unconfirmed_cities as $unconfirmed_city) {
    echo '<div>';
    echo $unconfirmed_city[name];
    echo '<input type="button" id= '.$unconfirmed_city[name].'" value="Accept" class="mybut btn btn-info btn-mini" style="">';
}

Comments

0
   echo '<input type="button" id= "'.$unconfirm_city[name].'" value="Accept" class="mybut btn btn-info btn-mini" style="">'

Check this

Comments

0

Try this:

<?php  
 if ($unconfirmed_cities) {  
        foreach ($unconfirmed_cities as $unconfirmed_city) {  
         echo '<div>';  
 echo "$unconfirmed_city[name]";  
 echo '<input type=\'button\' id= \''.$unconfirm_city[name].'\' value=\'Accept\' class=\'mybut btn btn-info btn-mini\' style=\'\'>';  
echo '</div>';  
} 

?>

Note: In \', (slash) is used as an escape sequence.

Comments

0

You forgot to escape the quotes, so $unconfirm_city is considered as string rather than variable by PHP.

Replace following line in your code,

echo '<input type="button" id= "$unconfirm_city[name]" value="Accept" class="mybut btn btn-info btn-mini" style="">';

by,

echo "<input type=\"button\" id=\"".$unconfirm_city['name']."\" value=\"Accept\" class=\"mybut btn btn-info btn-mini\" style=\"\">";

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.