Search code examples
phpajaxpostbase64content-type

Pass base 64 string using ajax script


i have use the following script to pass the string url to another php page.

            function saveIt(){

                var xmlhttp = new XMLHttpRequest();

                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        alert("Save successfully!");
                    }
                }

                var value1 = document.getElementById("url").value ;
                var value2 = document.getElementById("time").value;
                var value3 = document.getElementById("note").value;

               xmlhttp.open("POST", "insertFrame.php", true);
               xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
               xmlhttp.send("url="+ value1 + "&time=" + value2 + "&note=" + value3);



            }

After that, I insert the data retrieved into the table

$url = $_POST['url'];
$time = $_POST['time'];
$note = $_POST['note'];

mysql_connect("localhost", "root", "");
mysql_select_db("studioassessor_01");

mysql_query("INSERT INTO frame VALUES ('$url', '$time', '$note')");

But, when I want to retrieve the url from db to display the image, it failed and looks like corrupted.

    <?php

        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("studioassessor_01");

        $sql = mysql_query("SELECT * FROM frame");

        while($row = mysql_fetch_array($sql)){
            echo '<img src="' . $row['url'] . '">';

            echo "<br/><br/>";
            echo $row['note'];
        }

    ?>

After I have change the code around and testing, I found that the xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); is the possible cause that make my url become different value when pass to another php page and store in the db. May I know how can I going to solve this?


Solution

  • I have solve my question using str_replace(' ', '+', "your string"). This is because the string value when encoded will change all to space and the + sign is missing. So, I need to replace it back in order to get back the correct url value and display the image. Thank you.