How to insert javascript safely into database and take them back as it is?

admin

Administrator
Staff member
I'm making a WordPress Advertisement Management Plugin. Need the user to enter Google AdSense and other ad code into the database.

I checked the other solution like the
Code:
mysql_real_escape_string()
and
Code:
base64_encode()
&
Code:
base64_decode()
. But they are not doing the desired result.

I'm using a simple form like <a href="https://wordpress.stackexchange.com/a/108351/22728">this</a>:

Code:
&lt;form action="" enctype="multipart/form-data" method="post"&gt;
    &lt;textarea name="ad_code"&gt;&lt;/textarea&gt;
    &lt;input type="submit"&gt;
&lt;/form&gt;

To insert into the db:

Code:
&lt;?php
        if (!empty($_POST)) {
        global $wpdb;
            $table = $wpdb-&gt;my_table = $wpdb-&gt;prefix . "my_table";
            $data = array(
                'ad_code'    =&gt; $_POST['ad_code']
            );
            $format = array(
                '%s'
            );
            $success=$wpdb-&gt;insert( $table, $data, $format );
            if($success) echo 'data saved';
        }
?&gt;

With the aforementioned code, I tried with:

Code:
$data = array(
    'ad_code'    =&gt; mysql_real_escape_string( $_POST['ad_code'] )
);

and

Code:
$data = array(
    'ad_code'    =&gt; base64_encode( mysql_real_escape_string( $_POST['ad_code'] ) )
);

and

Code:
$data = array(
    'ad_code'    =&gt; base64_encode( $_POST['ad_code'] )
);

The first two attempt completely failed me. But using only the
Code:
base64_encode()
is doing a good job encoding the texts into code and inserting into the db, but when I'm trying with
Code:
&lt;?php echo "&lt;pre&gt;" . base64_decode( $the_ad_query-&gt;ad_code ) . "&lt;/pre&gt;"; ?&gt;
in my db query, it's not showing anything.

Instead of JavaScript, I tried with simple string like "This is the code", it's doing just fine. But I failed with JavaScript code.

My google adsense code is like this:

Code:
&lt;script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"&gt;&lt;/script&gt;
&lt;!-- my ad --&gt;
&lt;ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-32131313131312317"
     data-ad-slot="8657754785"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;

How can I proceed then? Any ideas?