Conkerface Conkerface - 6 months ago 15
PHP Question

use php to populate form fields

Ok, I'm at my wits end here. I'm making a website for a friend, to showcase his work. Each piece has it's own page. On those pages, we have a link

<a class="btn" href="../contact_2.html">Contact us about this item</a>


that takes us to the contact form page, where the user is expected to fill income details before getting in touch. What we want to do is have the 'subject' field filled with the name of the subject of the page the link was clicked form. i.e. you're on (../port_pages/38mmspruce.html), you click the "contact us about" link, and when you get to the page "contact.html", the subject field already has "38mmSpruce" populated.
As it stands, the form functions perfectly - the $_POST method is in use, and it sends the form as it should. However, trying to get the subject field to populate is driving me round the bend. The closest I got was with this html:

<label for="subject">Subject *</label>
<input id="subject" type="text" name="subject" class="form-control"
placeholder="Please enter the subject you'd like to discuss *"
value="<? php echo $_POST='subject'?>"
rows="1" required="required">


which fills the subject field with:

<? php echo $_POST='subject' ?>


rather than the ACTUAL subject. I've tried this also where I've changed every occurrence of $_POST to $_GET, but it returns the same result. If I change the anchor to:

<a class="btn" href="../contact.php">Contact us about this item</a>


It fires an email off without loading the form page. I've tried adding query strings to the anchor too, but they don't return any data. I know this must be really simple but I've been banging my head against it for nearly a week now, and despite trying to research every possible variation on this question, I can't find the right answer. Here is the code for the relevant pages:

contact.php

<?php


$from = 'Demo contact form <example@example.com>';
$sendTo = 'Demo contact form <example@example.com>';
$subject = $_POST['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'];
}


?>


conatact.js

$(function () {

$("#contact-form").submit(function () {

var url = "contact.php";


$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;

var alertBox = '<div class="alert ' + messageAlert + '
alert-dismissable"><button type="button" class="close" data-
dismiss="alert" aria-hidden="true">&times;</button>' + messageText +
'</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();

}
}
});
return false;
});

});


And finally, the form part of the html (edit. full page posted as requested)

<body>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">

<div class="navbar-header">
<button type="button" class="navbar-toggle" data-
toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Those That
Know</a>
</div>

<div class="collapse navbar-collapse" id="bs-example-navbar-
collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="mainportfolio.html">Portfolio</a>
</li>

<li class="active">
<a href="contact_2.html">Contact</a>
</li>


</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</nav>



<!-- Page Content -->
<div class="container">

<!-- Page Heading/Breadcrumbs -->
<div class="row">
<div class="col-lg-12 text-left">
<h1 class="page-header">Contact
<small>Get in touch</small>
</h1>
<ol class="breadcrumb">
<li><a href="index.html">Home</a>
</li>
<li class="active">Contact</li>
</ol>
</div>

</div>
<!-- /.row -->



<!-- Content Row -->

<!-- Map Column -->

<!-- Contact Details Column -->

<div class="row">
<div class="col-md-6">
<img class="img-responsive" src="images/About Main Pic.png"
alt="">
</div>
<div class="col-md-6">
<h2>Contact Details</h2>
<h2>Those That Know HQ<br>Wallsend-on-Tyne<br>
</h2>
<p><i class="fa fa-envelope-o"></i>
<abbr title="Email">E</abbr>: <a
href="mailto:name@example.com">example@example.com</a>
</p>
<p><i class="fa fa-clock-o"></i>
<abbr title="Hours">H</abbr>: Open 24 hours</p>

<ul class="list-unstyled list-inline list-social-icons">
<li>
<a class="btn btn-social-icon btn-facebook"
href="#"><i class="fa fa-
facebook-square fa-2x"></i></a>
</li>
<li>
<a class="btn btn-social-icon btn-envelope"><i
class="fa fa-envelope-square fa-2x"></i></a>
</li>
<li>
<a class="btn btn-social-icon-large btn-instagram"
href="#"><i class="fa fa-instagram fa-2x"></i></a>
</li>
<button onclick="goBack()">Back</button>

</div>
</div>

</ul>

</div>
</div>


<div class="container">

<div class="row">

<div class="col-lg-12">

<h1>Get in touch with <a href="contact_2.html">Those
That Know</a></h1>

<p class="lead">If you'd like to contact me about
placing an order, or about anything you've seen on the site, please
fill in the form below.</p>
<p>Please include information about the piece you've
been looking at. It'll help us to get started on your order.</p>


<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

*" required="required">
</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
*" required="required">
</div>

<div class="col-md-12">
<label for="form_subject">Subject *
</label>
<input id="form_subject" type="text"
name="subject" class="form-control" placeholder="Please enter the
subject you'd like to discuss *"
value="<? php echo $_POST['subject'];?
>"
rows="1" required="required">
</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" required="required"></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 required.</p>
</div>
</div>
</div>


</form>

</div><!-- /.8 -->

</div> <!-- /.row-->

<!-- /.container-->

<footer>
<div class="row">
<div class="col-lg-12">
<p>Copyright &copy; Those That Know 2016</p>
</div>
</div>
</footer>
<script>

function goBack() {
window.history.back();

}



</script>




<script src="https://code.jquery.com/jquery-1.12.0.min.js">
</script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/
bootstrap.min.js">
</script>
<script src="js/contact.js"></script>
</body>
</html>


As you can see, it's a Bootstrap form, and the .js and .php files came with it. I've changed the mail settings to real ones for testing to make sure it works, which it does, but I simply cannot figure out how to make the php 'echo' the subject from a page into the relevant field in the form. I've also removed all of my attempts at adding query strings after the page name in the link because I couldn't make any of them work. I really need a straight up answer to this puzzler-this is the 3rd time I've asked on here, and nothing is working. I think it's obvious that I have very weak skills, but I'm working on them and fixing this problem would really help me to move on to a new set of problems.
Your help is gratefully appreciated in advance.

Answer

Rename your contact_2.html to contact_2.php so it gets parsed by the PHP interpreter.

Change the href in

<a class="btn" href="../contact_2.html">Contact us about this item</a>

accordingly.

Finally replace your <? php echo $_POST='subject' ? > to

<?php if (!empty($_POST['subject'])) echo $_POST['subject']; ?>

like @manniL said.

With the comments below, @farmio was able to solve my problem. I had to change $_POST to $_GET and it all fell into place. Thank you @farmio