试图在两个html页面上传递一个值

时间:2017-05-23 19:06:54

标签: php html

我有一个带有表格的页面,可以通过电子邮件发送有关汽车配件的信息。例如,沃尔沃将有一个排气装置,发动机,内饰,沃克斯豪尔将有一些相同的领域和一些不同的等。 我转到沃尔沃部分并单击排气部门,它会将我带到我填写的表格并通过电子邮件将其通过电子邮件发送给沃尔沃。但是我试图将值从第一页传递到零件部分:例如,如果我点击排气,它将进入用户可以填写联系信息的表格,有问题的部分将填写按钮我刚刚点击了。

这是我的代码(第1页html):

    <tr>
      <td><a href="infovolvo.html"><img src="images/volvo.jpg" alt="Volvo" width="140" height="50" align="Top"></td>
      <td>exhaust </td>
      <td align="top"><form action="volvo.html"><button type="submit" name="parts" value="6">exhaust</button></form></td>
    </tr>
    <tr>
      <td><a href="infovolvo.html"><img src="images/volvo.jpg" alt="Volvo" width="140" height="50" align="Top"></td>
      <td>interior</td>
      <td align="top"><form action="volvo.html"><button type="submit" name="parts" value="7">interior</button></form></td>
    </tr>

然后在我的发送表单php文件中使用此开关,我认为该值将转移到volvo.html,然后通过电子邮件,但没有任何结果:

switch($_POST['parts']){
    case 6:
    $email_message  = "wide exhaust";
    break;
    case 7:
    $email_message  = "dual exhaust";
    break;
}

$first_name      = $_POST['first_name']; // required
$last_name       = $_POST['last_name']; // required
$email_from      = $_POST['email']; // required
$maintelephone   = $_POST['maintelephone'];
$mobiletelephone = $_POST['mobiletelephone']; // not required
$address         = $_POST['address'];
$postcode        = $_POST['postcode'];
$dob             = $_POST['dob'];
$contact         = $_POST['contact'];

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Main Number: ".clean_string($maintelephone)."\n";
$email_message .= "Mobile Number: ".clean_string($mobiletelephone)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "Postcode: ".clean_string($postcode)."\n";
$email_message .= "Date of Birth: ".clean_string($dob)."\n";
$email_message .= "Parts: ".clean_string($parts)."\n";
$email_message .= "Contact: ".clean_string($contact)."\n";

我当然拥有正确发送电子邮件所需的所有其他代码,我只是不想发布所有内容以使任何人对我特别遇到的问题感到困惑。

1 个答案:

答案 0 :(得分:-1)

如果您根据第一个HTML中的点击进行选择,那么最好使用带$_GET的超链接,如果您认为通过地址栏传输数据是安全的。然后在第二个HTML页面中,您只需在表单的隐藏输入中填写传输的值即可。以下是参考代码。

//This is 1st PHP where "sp" after "?" is short of Selected Part. You MUST use the value that will be stored in the DB or will be the Primary Key.
<a href="your_second_html_volvo.php?sp=Exhaust">Exhaust</a>

//2nd PHP - For instance selecting part of Volvo
<?php
    $selected_part = $_GET['sp'];
?>

<form action="your_action_script.php" method="POST">
    //Whatever details you want to collect
    <input type="text" name="customer_name" required>
    <input type="email" name="customer_email" required>

    //This is the Selected Part from the previous page
    <input type="hidden" name="selected_part" value=<?php echo "'" . $selected_part . "'" ?>>
</form>

现在,您可以使用$selected_part = $_POST['selected_part']

在操作脚本中轻松调用此选定值

希望这有帮助。