我的网站有多个html投资组合页面。每个页面都有指向contact.html页面的链接。我想要做的就是获得“主题”。联系表格的一行填写了一个ID,我已经附在每页的链接上。例如,如果用户在38mmSpruce.html'然后他们点击“与我们联系”这个项目'链接,当加载contact.html页面时,预填充联系表单的主题行。 我找了3天寻找解决方案是徒劳的;我试过onClick事件,我试图使用' _GET'在PHP中的功能,我尝试向主播添加字符串查询,但事实是,我只是不确定我做错了什么。 我知道代码很多,并且缺少重要的验证数据,但我只需要知道如何使这成为可能,以便我可以继续前进。老实说,它毁了我。这是我的contact.php表格
<?php
//I've changed these to dummies just to post this. It works with my email
$from = 'Demo contact form <example@example.com>';
$sendTo = 'Demo contact form <example@example.com>';
//I tried to change this to '_GET['subject'] but it didn't work
$subject = 'Subject';
$fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
$okMessage = 'Contact form succesfully submitted. Thank you, I will
get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please
try again later';
try {
$emailText = "You have new message from contact
form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
?>
我的html表单看起来像这样
<form id="contact-form" method="post" action="contact.php" name="contact-form" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<label for="form_name">Name *</label>
<input id="form_name" type="text"
name="name" class="form-control" placeholder="Please enter your name *"
=" ">
</div>
<div class="col-md-6">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" =" ">
</div>
<div class="col-md-12">
<label for="form_subject">Subject *</label>
<input id="form_subject" name="subject" class="form-control" placeholder="Please enter the subject you'd like to discuss *" rows="1" =" ">
</div>
<div class="col-md-12">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Your message here *" rows="4" =" "></textarea>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send
message">
</div>
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are .
</p>
</div>
</div>
</div>
</form>
如果有什么可以提供帮助我解决这个看似简单的谜题,请帮助我。 哦,总诺布,obvs。对于那个很抱歉。但我正在学习!
答案 0 :(得分:0)
如果您尝试将主题预加载到表单html中,则根本不需要更改表单处理代码。假设我正确理解您的代码,那么您应该这样做:
38mmSpruce.html
页面上的链接应如下所示:
http://example.com/contact.html?subject=the+subject+of+the+message
请注意URL编码的subject
查询字符串。
然后,在您的表单HTML(不您编写的用于处理表单数据的php)中,您只需设置输入字段的值:
<input id="form_subject" name="subject" class="form-control"
placeholder="Please enter the subject you'd like to discuss *"
value="<?php echo $_GET['subject']; ?>"
required="required"/>
注意subject
查询参数如何通过$_GET
访问,然后回显到输入值。
注意:您修改了代码中的拼写错误(尾随</textarea>
)...这也可能是您问题的一部分!
注意2:这也假设您的PHP处理了html文件,这绝对不是典型的。您可能需要将contact.html
更改为contact.php
。