0

JavaScript

<script>
function readURL(input) {
    if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        $('#blah').attr('src', e.target.result)
                  .width(150)
                  .height(200);
        };

        reader.readAsDataURL(input.files[0]);
    }
}
</script>

PHP

if($_POST) {
    $image = $_POST["image"];
    $pro_name = $_POST["pro_name"];
    $pro_desc = $_POST["pro_desc"];
    $price = $_POST["price"];
    $categories = $_POST["categories"];
    $stock = $_POST["stock"];

    $ekle=mysql_query("insert into product (`product_id`,`pro_name`,`pro_desc`,`price`,`categories`,`image`,`stock`) 
                       VALUES (NULL, '$pro_name','$pro_desc','$price','$categories','$image','$stock')");
    if($ekle) {
        echo "<script>alert('Product Added Succecfully.');window.location='add_pro.php'</script>";
    } else {
        echo "<script>alert('Unexpected Error, plase try again.');window.location='add_pro.php'</script>";
    }
}

HTML

<td width="175" height="225"><img id="blah" src="#" alt="your image" /></td>
<td><input type='file' onchange="readURL(this);" name="image" value=""/></td>

This HTML part shows the image on page, I need to take the image URL and send it to the database but I don't know how to do this in JavaScript.

gd1
  • 11,300
  • 7
  • 49
  • 88
orcanyedal
  • 21
  • 1
  • 3
  • 4
    **Warning:** you're using [a **deprecated** database API](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) yourself from. – Marcel Korpel Jan 04 '14 at 18:56
  • 3
    Note: jscript and javascript are [not the same thing](http://stackoverflow.com/questions/135203/whats-the-difference-between-javascript-and-jscript), so don't use "jscript" as a shortcut if you mean javascript. – Digital Chris Jan 04 '14 at 18:57

1 Answers1

0

Your current PHP code:

 $image = $_POST["image"];

is not using correct syntax.( change the $_POST["image"]; ) to $_FILE["image"];).

Try this:

 $image = $_FILE["image"];
Nemo
  • 352
  • 3
  • 13