Reputation: 527
I have these codes:
testing.php
<html>
<head>
<script type="text/javascript">
function copy_data(id){
var a = document.getElementById(id).value;
document.getElementById("copy_to").value=a;
}
</script>
</head>
<body>
<form action="testprocess.php" method="post">
<input type="text" name ="a" id="copy_from" onkeyup="copy_data('copy_from')"/>
<input type="text" value=0 name ="b" id="copy_to"/>
<input type="submit">
</form>
</body>
</html>
testprocess.php
<?php
$test = $_POST['copy_to'];
echo $test;
?>
I get an error saying that 'copy-to'
is an undefined variable. Can you please tell me why?
Thanks.
Upvotes: 0
Views: 95
Reputation: 1234
$_POST
will contain the values of your form fields based on the name
attribute of the form elements.
<input type="text" name="copy_from"/>
will become $_POST['copy_from']
and
<input type="text" name="copy_to"/>
will become $_POST['copy_to']
You are using the value in the id
attribute of the input (and spelling it inconsistently), so it is undefined to PHP.
Upvotes: 0
Reputation: 36955
$_POST
values are passed through an element's name
attribute rather than the ID. Try this:
<input type="text" value=0 name="copy_to" id="copy_to"/>
And make sure you use a an underscore in your PHP variable:
$test = $_POST['copy_to'];
Upvotes: 4
Reputation: 37504
Needs to be $_POST['a'] the id isn't submitted into the post array, it's the name attribute
Upvotes: 2
Reputation: 3615
It ought to be a _
instead of a -
.
EDIT: Oh god, it's even worse. It ought to be $_POST['a']
, because of the name
-attribute. The name-attribute is used to specify the name/identifier under which a GET or POST parameter will be passed to the web application. The id-attribute is mostly used to identify HTML elements at the client, e.g. when doing some stuff with javascript.
Upvotes: -1
Reputation:
because you have no element with name copy_to
in your form.
Try below :
<form action="testprocess.php" method="post">
<input type="text" name ="a" id="copy_from" onkeyup="copy_data('copy_from')"/>
<input type="text" value=0 name ="copy_to" id="copy_to"/>
<input type="submit">
</form>
Upvotes: 0