0

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.

1
  • Please read up on how to use forms and how to get the data from forms in PHP. Similar questions to this have been asked tons of time. They often end up as too localized because the real issue at hand is not searchable through the text provided. Commented Feb 5, 2012 at 0:48

5 Answers 5

4

$_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'];
Sign up to request clarification or add additional context in comments.

Comments

2

Needs to be $_POST['a'] the id isn't submitted into the post array, it's the name attribute

Comments

0

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>

1 Comment

@Renzo Montejo : have you checked with my updated answer just now ?
0

$_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.

Comments

-1

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.

1 Comment

This is a correction, but it's still wrong. The name attribute is sent in the request, not the id.

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.