Jump to content

php script not doing anything


cleary1981

Recommended Posts

Hi All

 

Im not so good with php, useless in fact. Im using jquery to pass an array to php. The php script should update records in a mysql db. The variables are all working in the javascript side but the records are not getting updated.

 

Ive included the jquery function I am using just to make things a little clearer

function render() {
alert(g_objName);
var arr = document.getElementsByTagName("div");
    var data = {
    'result[]' : [],
    	'xReturnValue[]': [],
    	'yReturnValue[]' : []
    	};
    
    for(var i = 0; i < arr.length; i++)
    	{
    	var xReturnValue = -10;
    	var yReturnValue = -5;
    	for(var elem = arr[i];elem != null;elem = elem.offsetParent)
    		{
    		xReturnValue += elem.offsetLeft;
   			yReturnValue += elem.offsetTop;
				           
   			}
   			alert(arr[i].innerHTML);
   			var result = arr[i].innerHTML;
    	data['result[]'].push(result);
    	data['xReturnValue[]'].push(xReturnValue);
    	alert(xReturnValue);
    	data['yReturnValue[]'].push(yReturnValue);
    	alert(yReturnValue);
    	}
    $.post("render.php",data);
    // this bit of code is using jquery 
}

 

heres the php

<?php
require "config.php";
foreach($_POST['result'] as $n=>$result){
  $xReturnValue = $_POST['xReturnValue'][$n];
  $yReturnValue = $_POST['yReturnValue'][$n];
  //do what you want with the data
$q = mysql_query("update object set xpos='$xReturnValue', ypos='$yReturnValue' where object_name = '$result'");

}

?>

Link to comment
https://forums.phpfreaks.com/topic/119447-php-script-not-doing-anything/
Share on other sites

shouldn't you first check if post data has been submitted before just rolling it out

 

<?php
require "config.php";
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'GET') {
  foreach($_POST['result'] as $n=>$result){
    $xReturnValue = $_POST['xReturnValue'][$n];
    $yReturnValue = $_POST['yReturnValue'][$n];
    //do what you want with the data
    $q = mysql_query("update object set xpos='$xReturnValue', ypos='$yReturnValue' where object_name = '$result'");
  }
}
?>

 

also try to debug using:

print_r($_POST);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.