0

I want to send information of 1 variable with javascript into PHP.

So , i used this code (in index.php) :

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    <script>
        $.post('http://localhost/test/index.php', {
            name: $('.class').html();
        });
        $(document).ready(function(){
           $("#my_form").on("submit", function () {
               var hvalue = $('.class').text();
               $(this).append("<input type='hidden' name='name' value=' " + hvalue + " '/>");
        });
    });
</script>
<form action="" method="post" id="my_form">
    <div class="class" name="name">
        this is my div
    </div>
    <input type="submit" value="submit" name="submit" />
</form>
<?php 
    if(isset($_POST['name'])){ $name = $_POST['name']; } 
    echo $name;

But i see this error :

Notice: Undefined variable: name in C:\Program Files\EasyPHP-5.4.0RC4\www\test\index.php on line 22

What can i do ?

2
  • please distinguish between java and javascript. they are, despite their names, NOT the same, not even roughly. Commented Feb 8, 2014 at 7:43
  • so many errors, so little time. Commented Feb 8, 2014 at 7:46

2 Answers 2

1

$name is not defined. You have the echo outside of the if statement, move it inside the braces.

if(isset($_POST['name'])) {
  $name = $_POST['name'];
  echo $name;
}

Also, you post to submit.php but this code is for index.php... so you need to fix that, too.

Sign up to request clarification or add additional context in comments.

1 Comment

I tested your code too and i have fixed the "submit.php" but it dorsn't work again, also my browser doesn't redirect to "localhost/test/submit.php" . what can i do now ?
0

also , In using jQuery , you didn't closed the tag :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

close like this and use this code :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.post('http://localhost/test/index.php', {
name: $('.class').html();
});
$(document).ready(function(){
   $("#my_form").on("submit", function () {
        var hvalue = $('.class').text();
        $(this).append("<input type='hidden' name='name' value=' " + hvalue + " '/>");
    });
});
</script>
<form action="submit.php" method="post" id="my_form">
<div class="class" name="name">
this is my div
</div>
<input type="submit" value="submit" name="submit" />
</form>

Comments

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.