php - 未接收$_POST[]参数

标签 php javascript

我试图通过 $_POST 方法将 test.php 中的值发送到 backgroundScript.php ,当打印出正在发送的值时,我得到了正确的值,但是当我转到 backgroundScript 文件查看时如果发送了值 - 没有打印任何内容,它等于 null --------------------------------test.php 文件- 主要代码

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  value='{$row['first']}'>{$row['first']}</option>";
 // echo getLN();

}

$dropdown .= "\r\n</select>";

echo $dropdown;


//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?>


<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

var dropdown = $('#contacts');

document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            $('#phone').val(response.phone);
            // Repeat for all of your form fields
        },
        'json'
    );

});

</script>

<form action="insert.php" method="post">
First Name: <input type="text" id="first"  ><br>
Last Name: <input type="text" id="last"><br>
Phone: <input type="text" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

backgroundScript.php ------------------------------------------------------ 哪里值已发送

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  

$first= $_POST['first'];
print_r("print value: $first");
//$first = "david";
$sth = $DBH->prepare('SELECT *
    FROM contacts
    WHERE first = :first');
$sth->bindParam(':first', $first, PDO::PARAM_STR);
$sth->execute();

$row = $sth->fetch();


$returnArray = array( 
'first' => $row["first"],
'last' => $row["last"], 
'phone' => $row["phone"], 
);
//print_r("After pureeing fruit, the colour is: $returnArray");
/*echo "<pre>";
print_r($returnArray);
echo "</pre>";
exit;*/


// background script

// retrieve data based on $_POST variable, set to $returnArray
/*while($row = $sth->fetch(PDO::FETCH_ASSOC)){
  // do something with row

}
$returnArray = array( 
'first' => $row["first"], 
);


echo "<pre>";
print_r($returnArray);
echo "</pre>";
exit;
*/



/****************************
 * the structure of returnArray should look something like
     array(
         'first' => firstName,
         'last' => lastName,

     )*/
    // echo json_encode(array('first' => "hello", 'last' => "Other value"));
//echo json_encode($returnArray);
$DBH = null; 
############################ 更新到 TEST.PHP
try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);


//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?>


<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

var dropdown = $('#contacts');

document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            $('#phone').val(response.phone);
            // Repeat for all of your form fields
        },
        'json'
    );

});

</script>

<form action="insert.php" method="post">
<?php

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  value='{$row['first']}'>{$row['first']}</option>";
 // echo getLN();

}

$dropdown .= "\r\n</select>";



echo $dropdown;
?>

First Name: <input type="text" name="first" id="first" ><br>
Last Name: <input type="text" id="last"><br>
Phone: <input type="text" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

现在,当我尝试打印下拉列表的值时,我在 JavaScript 中得到了未定义

######################################## 更新
<form action="backgroundScript.php" method="post">

<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

var dropdown = $('#contacts');

document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            $('#phone').val(response.phone);
            // Repeat for all of your form fields
        },
        'json'
    );

});

</script>


<?php

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  value='{$row['first']}'>{$row['first']}</option>";
}
$dropdown .= "\r\n</select>";

echo $dropdown;
?>

First Name: <input type="text" name="first" id="first" ><br>
Last Name: <input type="text" name="last" id="last"><br>
Phone: <input type="text"  name="phone" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

最佳答案

POST 使用name而不是id来识别参数。使用name在您的字段上 - 或两者都使用。

<input type="text" name="first" id="first" >

Javascript 可以很好地处理整个 ID getElementByID但 POST 和 GET 数据需要 name待使用。

编辑:您还需要确保 <select....>声明属于 <form> 内和</form>标签,否则它不会作为该表单的一部分发送。

粘贴此行:

<form action="backgroundScript.php" method="post">

以上几行:

<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

编辑 2:此外,正如 Herpa 指出的那样,您的 <form action=''>设置为insert.php而不是backgroundScript.php 。您也需要改变这一点。

关于php - 未接收$_POST[]参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11859096/

相关文章:

php - 不包含图像/img/溢出的圆 Angular : hidden;

java - Cakephp Json 输出令人困惑

javascript - 在无限循环中运行函数

javascript - 使用openpgp.js计算公钥的 key id

php - 在 PHP 中创建搜索引擎

php - 选择给定月份发生的事件

php - 测试应用程序/数据库的多语言支持

javascript - 如何从firebase上 Node 的父键获取子键

javascript - CSS 中的动画文本移动

javascript - 如何将 AJAX 字符串响应转换为 JSON