Lucas Frugier
Lucas Frugier

Reputation: 287

include php inside json

I have table (data) and I need to insert some php inside

How do I do that?

my code :

$data['produit'] =  '       
        <label for="designation" class="col-sm-1 control-label">Désignation</label>
        <div class="col-sm-3">
            <input type="text" class="form-control" name="NEED PHP HERE FOR EXAMPLE" id="designation" readonly>
        </div>
        <label for="prix" class="col-sm-1 control-label">Prix </label>
        <div class="input-group col-sm-1">
            <input type="text" class="form-control" aria-describedby="basic-addon2" name="prix" id="prix" readonly>
            <span class="input-group-addon" id="basic-addon2">€</span>
        </div>
        ';

i need to do something like that :

$data['produit'] =  ' <input type="text" class="form-control" 
    name="' . echo 'test' . '" id="designation" readonly>';

Upvotes: 0

Views: 334

Answers (2)

Robert Wade
Robert Wade

Reputation: 5003

You just need to concatenate your string with your PHP variables. In php the concatenation operator is a period (.)

Here's an example that inserts a variable containing my name in your string, as well as a function that returns your name being inserted in your string.

$myName = "Robert";

function yourName() {
    return "Lucas";
}

$data['produit'] =  '<label for="designation" class="col-sm-1 control-label">' . $myName . '</label><div class="col-sm-3"><input type="text" class="form-control" name="NEED PHP HERE FOR EXAMPLE" id="designation" readonly></div><label for="prix" class="col-sm-1 control-label">' . yourName() . ' </label><div class="input-group col-sm-1"><input type="text" class="form-control" aria-describedby="basic-addon2" name="prix" id="prix" readonly><span class="input-group-addon" id="basic-addon2">€</span></div>';

Upvotes: 2

MrMisery
MrMisery

Reputation: 416

you can't do it this way , since php is the server language it is compiled before the (HTML,JS...) parts, instead you can produce the results you wish within the server part and send it using JSON.

Upvotes: 0

Related Questions