Avoid
Avoid

Reputation: 353

how to assign PHP variable in a button ID

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.

Upvotes: 0

Views: 10030

Answers (5)

GautamD31
GautamD31

Reputation: 28763

Try like

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

Upvotes: 2

aamir
aamir

Reputation: 4043

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=\"\">";

Upvotes: 0

Prashanth
Prashanth

Reputation: 1304

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.

Upvotes: 0

Naeem
Naeem

Reputation: 36

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

Check this

Upvotes: 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="">';
}

Upvotes: 0

Related Questions