lundi 19 octobre 2015

Using Javascript function to alter PHP variable and output

I am currently an array and outputting the contents to a table as follows

<table>
<thead>
<tr class="tblhead">
<th width=>ID</th>
<th width=>Email</th>
<th>Name</th>
<th width=>Websites</th>
</tr>
</thead>
<tbody>

@foreach ($app as $application)
<tr>
  <td><a href="">{{$application->id}}</a></td>
  <td>{{$application->email or ''}}<br />
    {{ $application->referred_by or '' }}
  </td>
  <td>{{ $application->first_name or ''}}
  </td>
  <td>{!! $application->websites !!}</td>
</tr>
@endforeach
 </tbody>
</table>

So what I was doing before is in my Controller, I would loop through all the websites fields and do preg_replace to replace all urls to inside anchor tags. But this caused a problem in blade because I had to escape the html entities. I realized this is potential for XSS attack.

I want to now take that $application->websites variable on the Blade template and send it to a javascript function on this same page that will convert the part of the text that is a url to a hyperlink and then return to be outputted in Blade.

The only problem is I have no experience with JS especially while using PHP with it. I have a function that should replace the text to hyperlink here.

function link(text) {
var urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(urlRegex, function(url) {
    return '<a href="' + url + '">' + url + '</a>';
});
}

How do I send the php variable to this function and get it back to ouput on blade?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire