1

I'm trying to write a form that will update the hidden field values and then submit, after which the values will be input into the mysql database. However, while the form does seem to submit, the $_POST array seems to be empty and i get "unidentified index" errors whenever i try to access any of the $_POST elements??

The relevant code is as follows:

<?php
if(isset($_GET['a'])){
$title = $_POST['left'];
$sql = "UPDATE boxes SET topx = '".$_POST['left']."', topy = '".$_POST['top']."', width = '".$_POST['width']."', height = '".$_POST['height']."' WHERE id = '2'";
    // this is where I get "unidentified index" errors
mysql_query($sql);
}
else {
$title = "test";
}
?>

the JS function to fill the hidden fields and submit the form:

<script type = "text/javascript">
function dosmt(form){
JStopx = dd.elements.field2.x; alert(JStopx);
JStopy = dd.elements.field2.y; alert(JStopy);
JSwidth = dd.elements.field2.w; alert(JSwidth);
JSheight = dd.elements.field2.h; alert(JSheight);
alert("test2");

alert(document.getElementById('top').value);
document.getElementById('top').value = JStopy; alert("test1");
document.getElementById('left').value = JStopx;
document.getElementById('width').value = JSwidth;
document.getElementById('height').value = JSheight;
alert("waah");
location = "http://127.0.0.1/experiment/index.php?a=true";
alert ("OK"); 
document.getElementById('testform').submit();
}
</script>

and the form:

<form name = 'testform' method = "post" id = 'testform' action = "index.php">
<input type = "hidden" name = 'top' id = 'top' value = ''/>
<input type = "hidden" name = 'left' id = 'left' value = ''/>
<input type = "hidden" name = 'width' id = 'width' value = ''/>
<input type = "hidden" name = 'height' id = 'height' value = ''/>
<input type = "hidden" name = 'placeholder' id = 'placeholder' value = 'blah'/>
<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(this.form)'>
</form>

Any help would be appreciated, thanks!

1

3 Answers 3

2

You shouldn't use location in JavaScript; if you do something like

location='http://example.com'

you're actually redirecting the page to example.com.

You should remove the line

location = "http://127.0.0.1/experiment/index.php?a=true";

and change it to something like:

document.getElementById('testform').action="http://127.0.0.1/experiment/index.php?a=true";
Sign up to request clarification or add additional context in comments.

Comments

1

When you submit your form, you go to index.php, not index.php?a=true - try doing

location = "http://127.0.0.1/experiment/index.php?a=true";
document.getElementById('testform').action = location;

5 Comments

check for js warnings/errors in firebug, check output of var_dump($_POST) and var_dump($_GET)
output of var_dump($_POST) is "array(0) { } "
i got one error after pressing the submit button that said "prompt aborted by user" and "throw Components.Exception("prompt aborted by user", Cr.NS_ERROR_NOT_AVAILABLE) nsPrompter.js (line 457) <System>" not too sure what this means though
Don't do that. You can't use location as the name of the variable; assigning anything to location will redirect you to that url! See the MDN: developer.mozilla.org/en/DOM/window.location
0

Replace

<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(this.form)'>

with

<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(document.testform)'>

1 Comment

changing it with "document.testform" also does not seem to work :( i am pretty sure the function calls because I tested the hidden fields to see if they were updated (by changing the hidden fields to text fields) and they did get updated, its just that they dont get sent as post data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.