没有收到$ _POST []参数

时间:2012-08-08 06:49:00

标签: 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 ------------------------------------------- where值已发送

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>

2 个答案:

答案 0 :(得分:3)

POST使用name代替id来识别参数。在您的字段上使用name - 或使用两者。

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

Javascript可以很好地处理整个getElementByID的ID,但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:另外,正如赫伯指出的那样,您的<form action=''>设置为insert.php而不是backgroundScript.php。你也需要改变它。

答案 1 :(得分:1)

在test.php文件中更改此内容:

<form action="backgroundScript.php.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>
<?php echo $dropdown; ?>
<input type="Submit">
</form>

表单标记的action属性显示POST数据的发送位置。在您的脚本中,您将其发送到insert.php。您还应该使用name属性通过POST变量获取输入字段的名称。 编辑:您正在回显表单外的选择。