0

I have to add a url into a data-url attribute in a button element. But from the url image, I have to add id and book name which are php variables. How can I add this two php variables in my data-url attribute?

This is the URL : http://localhost/book%20store/detail.php?id=53&cat=Game Of Thrones This is my code (I've tried this but this is not working):

$book_id = $row['b_id'];  
$book_name = $row['b_nm'];    
echo'<table>  
     <tr>  
       <button class="sharer btn btn-lg"   
data-url="http://localhost/book store/detail.php?id={$book_id}&cat={$book_name}" >
       Share to Facebook </button>
     </tr>
      </table>';

from the url : $row['b_id'] = 53
And $row['b_nm']=Game Of Thrones

1
  • Removed image and Added code to my question. Commented Dec 29, 2016 at 19:36

2 Answers 2

1

Assuming your vars aren't empty then the below should work, your orig wont work as it has single quotes and the vars inside. So use double quotes or the solution in my post below, either should work

    echo '<table>  
     <tr>  
       <button class="sharer btn btn-lg"   
data-url="http://localhost/book store/detail.php?id=' . $book_id . '&cat=' . $book_name . '" >
       Share to Facebook </button>
     </tr>
      </table>';
Sign up to request clarification or add additional context in comments.

1 Comment

oops.. forgot to do that. Done now. Thanks .
0

I'm not sure I'm understanding your question correctly, but if you are trying to insert PHP variables into a HTML element in your PHP page, you could do it inline like this:

<a href="..." data-url="http://books.matrixmp3bd.com/book%20store/detail.php?id=<?PHP echo $bookId; ?>"></a>

$bookId is whatever your book id variable is.

Please see this question:

Using PHP variables inside HTML tags?

Edit in response to OP's comment and code snippet

In the line of the code you provided, you would just add your $book_id variable in-line to your echo statement:

detail.php?id="'.$book_Id.'"> Share to</button>

Notice I used the . operator to append the variable into the string you were already echoing.

3 Comments

I have the code which looks like this : <?php $book_id = $row['b_id']; $book_name = $row['b_nm']; echo'<table> <tr> <button class="sharer btn btn-lg" data- url="localhost/book store/detail.php?id=" > Share to Facebook </button> </tr> </table>'; Now i don't understand how can i insert the $book_id and $book_name varibale into the data-url.
That is not not working. After i shared the post, the url looks like this : "localhost/book%20store/detail.php?id" no id value is showing to the url.. I have tried this : data-url="localhost/book store/detail.php?id={$book_id}&cat={$book_name}" > Share to Facebook </button> but this also isn't working. Cannot understand the reason. :(
Did you verify that the $book_id and $book_name variables aren't empty? The syntax for the inline variables in my answer is correct and should work, you can also check the other answer I linked to, it shows two different ways of doing it that should work.

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.