Jump to content

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.