Hello stackexchange社区,
我正在尝试使用node.js脚本将post变量传递到php页面,当post vars生成页面时,页面自动使用document.getElementById('form1')提交.Click();
我知道php正在运行,因为在我的终端中我可以看到预先填充的所有值。但是我的javascript不会自动将表单提交到其他页面。
知道我做错了吗?
<?php
$firstname = (isset($_POST['firstname']))?$_POST['firstname']:'';
$lastname = (isset($_POST['lastname']))?$_POST['lastname']:'';
$address1 =(isset($_POST['address1']))? $_POST['address1']:'';
$city =(isset($_POST['city']))? $_POST['city']:'';
$state = (isset($_POST['state']))?$_POST['state']:'';
$zip = (isset($_POST['zip']))?$_POST['zip']:'';
$phone =(isset($_POST['phone1']))? $_POST['phone1']:'';
$email =(isset($_POST['email']))? $_POST['email']:'';
$ApptDate = (isset($_POST['ApptDate']))?$_POST['ApptDate']:'';
$ApptTime = (isset($_POST['ApptTime']))?$_POST['ApptTime']:'';
$notes = (isset($_POST['notes']))?$_POST['notes']:'';
$sender = (isset($_POST['sender']))?$_POST['sender']:'';
?>
<?php
if (isset($_POST['firstname']))
{?>
<html>
<head>
</head>
<body>
<form name='form1' id='form1' action='http://hg06.leadperfection.com/batch/addleads.asp' method='POST'>
<table border=1 cellspacing=0 callpadding=0>
<tr><td>FirstName</td><td><input type='Text' id='firstname' name='firstname' maxlength='25' value="<?php echo $firstname; ?>"></td></tr>
<tr><td>LastName</td><td><input type='Text' id='lastname' name='lastname' maxlength='25' value="<?php echo $lastname; ?>"></td></tr>
<tr><td>Address1</td><td><input type='Text' id='address1' name='address1' maxlength='35' value="<?php echo $address1; ?>"></td></tr>
<tr><td>City</td><td><input type='Text' id='city' name='city' maxlength='35' value="<?php echo $city; ?>"></td></tr>
<tr><td>State</td><td><input type='Text' id='state' name='state' maxlength='2' value="<?php echo $state; ?>"></td></tr>
<tr><td>Zip</td><td><input type='Text' id='zip' name='zip' maxlength='15' value="<?php echo $zip; ?>"></td></tr>
<tr><td>Phone1</td><td><input type='Text' id='phone1' name='phone1' maxlength='15' value="<?php echo $phone1; ?>"></td></tr>
<tr><td>Phone2</td><td><input type='Text' id='phone2' name='phone2' maxlength='15' value="<?php echo $first; ?>"></td></tr>
<tr><td>Phone2Type</td><td><input type='Text' id='phone2type' name='phone2type' maxlength='2' value="<?php echo $first; ?>"></td></tr>
<tr><td>Phone3</td><td><input type='Text' id='phone3' name='phone3' maxlength='15' value="<?php echo $first; ?>"></td></tr>
<tr><td>Phone3Type</td><td><input type='Text' id='phone3type' name='phone3type' maxlength='2' value="<?php echo $first; ?>"></td></tr>
<tr><td>Product</td><td><input type='Text' id='productid' name='productid' maxlength='10' value="<?php echo $first; ?>"></td></tr>
<tr><td>ProdDescr</td><td><input type='Text' id='proddescr' name='proddescr' maxlength='20' value="<?php echo $first; ?>"></td></tr>
<tr><td>Email</td><td><input type='Text' id='email' name='email' maxlength='100' value="<?php echo $email; ?>"></td></tr>
<tr><td>LogNumber</td><td><input type='Text' id='LogNumber' name='LogNumber' maxlength='50' value="<?php echo $first; ?>"></td></tr>
<tr><td>Sender</td><td><input type='Text' id='sender' name='sender' maxlength='100' value="<?php echo $sender; ?>"></td></tr>
<tr><td>Sent To</td><td><input type='Text' id='sentto' name='sentto' maxlength='100' value="<?php echo $first; ?>"></td></tr>
<tr><td>Call Morning (Y/N)</td><td><input type='Text' id='callmorning' name='callmorning' maxlength='1' value="<?php echo $first; ?>"></td></tr>
<tr><td>Call Afternoon (Y/N)</td><td><input type='Text' id='callafternoon' name='callafternoon' maxlength='1' value="<?php echo $first; ?>"></td></tr>
<tr><td>Call Weekend (Y/N)</td><td><input type='Text' id='callevening' name='callevening' maxlength='1' value="<?php echo $first; ?>"></td></tr>
<tr><td>Call Evening (Y/N)</td><td><input type='Text' id='callweekend' name=callweekend' maxlength='1' value="<?php echo $first; ?>"></td></tr>
<tr><td>ApptDate</td><td><input type='Text' id='ApptDate' name='ApptDate' maxlength='20' value="<?php echo $ApptDate; ?>"></td></tr>
<tr><td>ApptTime</td><td><input type='Text' id='ApptTime' name='ApptTime' maxlength='20' value="<?php echo $ApptTime; ?>"></td></tr>
<tr><td>ProID</td><td><input type='Text' id='ProID' name='ProID' maxlength='5' value="<?php echo $first; ?>"></td></tr>
<tr><td>Srs-Sub ID#</td><td><input type='Text' id='srs_id' name='srs_id' maxlength='5' value="<?php echo $first; ?>"></td></tr>
<tr><td>Notes</td><td><input type='Text' id='notes' name='notes' maxlength='2000' value="<?php echo $notes; ?>"></td></tr>
</table>
<input name="btnSubmit" type="submit" value="Submit" />
</form>
<script>document.getElementById('form1').click();</script>
</body>
</html>
<?php
}
else
{
// leave the page alone
}
?>
这是我的终端输出,确认post变量实际上正在点击页面。正如您所见,填充了值,并根据php生成页面。
<html>
<head>
</head>
<body>
<form name='form1' id='form1' action='http://hg06.leadperfection.com/batch/addleads.asp' method='POST'>
<table border=1 cellspacing=0 callpadding=0>
<tr><td>FirstName</td><td><input type='Text' id='firstname' name='firstname' maxlength='25' value="alan"></td></tr>
<tr><td>LastName</td><td><input type='Text' id='lastname' name='lastname' maxlength='25' value="huber"></td></tr>
<tr><td>Address1</td><td><input type='Text' id='address1' name='address1' maxlength='35' value="east 123 hello st"></td></tr>
<tr><td>City</td><td><input type='Text' id='city' name='city' maxlength='35' value="roscoe"></td></tr>
<tr><td>State</td><td><input type='Text' id='state' name='state' maxlength='2' value="IL"></td></tr>
<tr><td>Zip</td><td><input type='Text' id='zip' name='zip' maxlength='15' value="61073"></td></tr>
<tr><td>Phone1</td><td><input type='Text' id='phone1' name='phone1' maxlength='15' value=""></td></tr>
<tr><td>Phone2</td><td><input type='Text' id='phone2' name='phone2' maxlength='15' value=""></td></tr>
<tr><td>Phone2Type</td><td><input type='Text' id='phone2type' name='phone2type' maxlength='2' value=""></td></tr>
<tr><td>Phone3</td><td><input type='Text' id='phone3' name='phone3' maxlength='15' value=""></td></tr>
<tr><td>Phone3Type</td><td><input type='Text' id='phone3type' name='phone3type' maxlength='2' value=""></td></tr>
<tr><td>Product</td><td><input type='Text' id='productid' name='productid' maxlength='10' value=""></td></tr>
<tr><td>ProdDescr</td><td><input type='Text' id='proddescr' name='proddescr' maxlength='20' value=""></td></tr>
<tr><td>Email</td><td><input type='Text' id='email' name='email' maxlength='100' value="aghuber11@gmail.com"></td></tr>
<tr><td>LogNumber</td><td><input type='Text' id='LogNumber' name='LogNumber' maxlength='50' value=""></td></tr>
<tr><td>Sender</td><td><input type='Text' id='sender' name='sender' maxlength='100' value="InboundProspect"></td></tr>
<tr><td>Sent To</td><td><input type='Text' id='sentto' name='sentto' maxlength='100' value=""></td></tr>
<tr><td>Call Morning (Y/N)</td><td><input type='Text' id='callmorning' name='callmorning' maxlength='1' value=""></td></tr>
<tr><td>Call Afternoon (Y/N)</td><td><input type='Text' id='callafternoon' name='callafternoon' maxlength='1' value=""></td></tr>
<tr><td>Call Weekend (Y/N)</td><td><input type='Text' id='callevening' name='callevening' maxlength='1' value=""></td></tr>
<tr><td>Call Evening (Y/N)</td><td><input type='Text' id='callweekend' name=callweekend' maxlength='1' value=""></td></tr>
<tr><td>ApptDate</td><td><input type='Text' id='ApptDate' name='ApptDate' maxlength='20' value="2016-12-01"></td></tr>
<tr><td>ApptTime</td><td><input type='Text' id='ApptTime' name='ApptTime' maxlength='20' value="2pm"></td></tr>
<tr><td>ProID</td><td><input type='Text' id='ProID' name='ProID' maxlength='5' value=""></td></tr>
<tr><td>Srs-Sub ID#</td><td><input type='Text' id='srs_id' name='srs_id' maxlength='5' value=""></td></tr>
<tr><td>Notes</td><td><input type='Text' id='notes' name='notes' maxlength='2000' value="this is a note"></td></tr>
</table>
<input name="btnSubmit" type="submit" value="Submit" />
</form>
<script>document.getElementById('form1').click();</script>
</body>
</html>
答案 0 :(得分:1)
你应该在你的情况下使用cURL:
<?php
if (isset($_POST['firstname'])) {
$data['firstname'] = (isset($_POST['firstname']))?$_POST['firstname']:'';
$data['lastname'] = (isset($_POST['lastname']))?$_POST['lastname']:'';
$data['$address1'] =(isset($_POST['address1']))? $_POST['address1']:'';
$data['$city'] =(isset($_POST['city']))? $_POST['city']:'';
$data['state'] = (isset($_POST['state']))?$_POST['state']:'';
$data['zip'] = (isset($_POST['zip']))?$_POST['zip']:'';
$data['phone'] =(isset($_POST['phone1']))? $_POST['phone1']:'';
$data['email'] =(isset($_POST['email']))? $_POST['email']:'';
$data['ApptDate'] = (isset($_POST['ApptDate']))?$_POST['ApptDate']:'';
$data['ApptTime'] = (isset($_POST['ApptTime']))?$_POST['ApptTime']:'';
$data['notes'] = (isset($_POST['notes']))?$_POST['notes']:'';
$data['sender'] = (isset($_POST['sender']))?$_POST['sender']:'';
$url = 'the_url_I_want_to_send_post_data';
$c = curl_init();
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($c);
curl_close($c);
var_dump($response); //just for debug, remove when in production
}