簡體   English   中英

通過JavaScript將信息發送到PHP

[英]Sending information by javascript into PHP

我想使用JavaScript將1個變量的信息發送到PHP。

因此,我使用了此代碼(在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;

但是我看到這個錯誤:

注意:未定義變量:第22行的C:\\ Program Files \\ EasyPHP-5.4.0RC4 \\ www \\ test \\ index.php中的名稱

我能做什么 ?

$name未定義。 您可以將回顯放在if語句之外,然后將其移至大括號內。

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

另外,您發布了commit.php,但是此代碼用於index.php ...,因此您也需要修復該問題。

另外,在使用jQuery時,您沒有關閉標簽:

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

像這樣關閉並使用以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM