javascript - AJAX and PHP, wont send data -


i have kind of weird problem, can't seem solve.

what trying do, call jquery function on 1 page, send argument of function .php-file on same server, have php file read variable, , insert database.

the function trigger placed on div, , looks this:

<div class="add_button" onclick="addimg('<?php echo $value[id]; ?>');"> 

the function looks this:

function addimg(id) {         $.ajax({             url: "addimgs.php",             type: "post",             data: {"id" : id}         });          console.log("function called id: " + id); } 

i using console try , debug little here, , console.log()-function being executed correctly. log give me error 500 if "url" not exist.

lastly, php-code of addimgs.php looks this:

<?php  $id = $_request['id'];   $servername = "localhost"; $username = "username"; $password = "password";  // create connection $conn = new mysqli($servername, $username, $password, "db_name");  $query = "insert imgs (id) values ($id)"; $result = $conn->query($query);  $conn->close();  ?> 

this .php-file works fine, if add

?id=123 

to end of it's url, , value of "123" added table in database.

so assumption problem connection between ajax function, , php-file.

please :)

in question variable name id, on actual website gave link name img_id.

function addimg(id) {         $.ajax({             url: "http://projects.mn-web.dk/instafetcher/addimgs.php",             type: "post",             data: {"img_id" : id}         });          console.log("function called id: " + id); } 

are sure server side script expects img_id? in particular, sure in actual php script have line:

$id = $_request['img_id']; 

and not this:

$id = $_request['id']; 

also note image ids not integers:

img_id="828281229958329915_2834748" 

so make sure type of id in imgs varchar or else can store strings, , not int or bigint.

update: ivan pointed out in comment, since img_id string, not integer, insert query invalid, needs enclose id quotes. 1 simple way is:

$query = "insert imgs (id) values (\"".mysqli_real_escape_string($id)."\")"; 

but more proper way use prepared statements:

http://php.net/manual/en/mysqli.prepare.php


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -