twan twan - 3 months ago 11
PHP Question

Passing a href as string through hidden input field (value)

I'm trying to pass some data through a hidden input field. I want every part to be wrapped by an anchor tag that links to the correct page. But when I send it the hidden values are displayed probably because sending an anchor tag through a value field is not allowed. How can I do this?

Code:

foreach($offertecr as $offerte1){
if($offerte1['id'] != ''){
$message .= '<a href="#">'.$offerte1['title'].'</a><br>';
}
}

<input type="hidden" class="form-control-products" name="products" id="products" value="'.$message.'">


This results in the following:

enter image description here

I probably have to use javascript for this?

Answer

You can use htmlspecialchars to convert the html tags into entities so that they can be properly displayed in your form.

foreach($offertecr as $offerte1){
    if($offerte1['id'] != ''){
        $message .= htmlspecialchars('<a href="#">'.$offerte1['title'].'</a><br>');
    }
}

<input type="hidden" class="form-control-products" name="products" id="products" value="'.$message.'">

When you obtain the posted information, you will have to convert the entities back into their corresponding tags using htmlspecialchars_decode.

$html_value = htmlspecialchars_decode($posted_value);

On the other hand, if you just want the information of the urls that point to the correct pages you can just skip the anchor tags and add only a comma separated value of the urls. You can later explode explode(',', $values) those values when you post the form to get the urls back.

Comments