I have built a inquiry form on my website, the idea is instead of mailing me each time a user submits a query it is added to my database which I can then go and view via my backend system
Each query will be listed one by one with a text-area contact form below it allowing me to reply to each query individually
So far I have this (sorry it's a bit messy)
foreach ($listings as $row){
$loop.= "<h3 class='text-center'>".$row['question']."</h3>";
$loop.= "<p>".$row['message']."</p>";
$loop.= "Name: <b>".$row['name']."</b>";
$loop.= "<span class='pull-right'>Email: <b>".$row['email']."</b><br></span>";
$loop.= "<div class='clearfix'></div>";
if(isset($row['website'])){ $loop.="Website: <b>".$row['website']."</b>"; }
$loop.= "<span class='pull-right'>Date: <b>".$row['date']."</b></span>";
$loop.= "<form name='submit-response' method='POST'><fieldset>";
$loop.= "<div class='form-group'> <label for='Message".$counter."'>Your Message</label> <textarea id='Message".$counter."' name='Message".$counter."' class='form-control' rows='5'></textarea> </div>";
$loop.= "<button type='submit' name='submit".$counter."' class='btn btn-default btn-block'>Reply</button>";
$loop.= "</fieldset></form>";
}
Before that is a foreach loop and the start of the oh and $counter is set to nill
What I want is for each contact form to be unique so when I click send on one of the queries it will be sent and removed so I can send another, the only issue I am having is working out how I will work out if a submit has been hit, and which submit has been hit
The code will need to workout which button has been hit and depending on which button it will then mail() to the recipient
I'm quite stuck on this one and I'm not sure of the best course of action so any advice is really appreciated
Luke
If you click a submit button inside a <form> tag, then only that form will be submitted.
You could include a hidden field with the ID of the row in it. That way you could get rid of the $counter variables altogether.
Also if you plan on just echoing out the $loop html, I wouldn't recommend storing the HTML in a PHP variable.
<?php
foreach ($listings as $row)
{
?>
<h3 class="text-center"><?php echo $row['question']; ?></h3>
<p><?php echo $row['message']; ?></p>
Name: <b><?php echo $row['name']; ?></b>
<span class="pull-right">Email: <b><?php echo $row['email']; ?></b><br></span>
<div class="clearfix"></div>
<?php
if(isset($row['website']))
{
?>
Website: <b><?php echo $row['website']; ?></b>
<?php
}
?>
<span class="pull-right">Date: <b><?php echo $row['date']; ?></b></span>
<form action="" name="submit-response" method="POST">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<fieldset>
<div class="form-group">
<label>Your Message</label>
<textarea name="Message" class="form-control" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-default btn-block">Reply</button>
</fieldset>
</form>
<?php
}
?>
add a unique id to your database table, and put it in a hidden input.
Give each form a id, and possibly each submit button a unique name. This way you can easily determine which submit button was hit, or which form was submitted, and remove it or process it via javascript.
$('form').each(function() {
$(this).submit(function(event) {
event.preventDefault();
// Add AJAX code here
$(this).remove();
});
});
Of course that was pseudo-code.
Related
Im building a simple element to show search results. I'm using a php function, so I can while-loop the results and display them.
The function is named search_results and looks like this:
<?php
function search_results($u_country, $user_name, $business_user, $brand_name, $product_name, $up_condition, $up_commentary, $up_price, $up_shipping, $up_amount, $up_id) {
$element = "
<div>
<form action='search.php' method='post'>
<div class='search_body'>
<span class='country'>$u_country</span>
<span class='username'>$user_name</span>
<span class='business_user'>$business_user</span>
<span class='product_name'>$product_name</span>
<span class='up_condition'>$up_condition</span>
<span class='commentary'>$up_commentary</span>
<span class='price'>$up_price</span>
<span class='amount'>$up_amount</span>
<span>
<input type='number' name='up_amount' min='1' max='$up_amount' placeholder='1'>
</span>
<span>
<input type='hidden' name='product_id' value='$up_id'>
</span>
<span>
<button type='submit' name='add_cart' disabled='".if(!isset($_SESSION['username']))."'>Warenkorb</button>
</span>
</div>
</form>
</div>
";
echo $element;
};
But I want to implement some php in some of the attributes. For example I want to disable the "add_cart" button if the user is not logged in. But if i use some php inside the html form it always crushes.
if I put the form in single quotes and every attribute in double quotes it won't translate the variables.
my understanding is that i have to end a html string with " and use a . to separate it from the php part, but I can't make it work.
Is there something I'm missing? is it not possible to mix in php into a html form while inside a function?
couldn't find a solution for this and looked through the other threads but couldn't find anything relative.
Sorry if this is a dumb question I'm still learning to code.
if I put the form in single quotes and every attribute in double quotes it won't translate the variables.
I want to further use php inside the attributes of the form to make the results more interactive. For example i want the displayed name of the product to be a link to the product page so it will need to use a variable for that.
Break you code up into bits that do works and use $element .= .... to concatenate the working bits into the variable
For example
function search_results($u_country, $user_name, $business_user, $brand_name,
$product_name, $up_condition, $up_commentary, $up_price,
$up_shipping, $up_amount, $up_id)
{
$element = "<div>
<form action='search.php' method='post'>
<div class='search_body'>";
$element .= "<span class='country'>$u_country</span>
<span class='username'>$user_name</span>
<span class='business_user'>$business_user</span>
<span class='product_name'>$product_name</span>
<span class='up_condition'>$up_condition</span>
<span class='commentary'>$up_commentary</span>
<span class='price'>$up_price</span>
<span class='amount'>$up_amount</span>
<span>
<input type='number' name='up_amount' min='1' max='$up_amount' placeholder='1'>
</span>
<span>
<input type='hidden' name='product_id' value='$up_id'>
</span>
<span>";
$t = ''; // default to not disabled
if(!isset($_SESSION['username'])){
$t = " disabled='disabled' ";
}
$element .= "<button type='submit' name='add_cart' $t>Warenkorb</button>
</span>
</div>
</form>
</div>";
echo $element;
}
step 1: run the code (the button will be disabled)
step 2 refresh the page then the button is not anymore disabled(f5 key,..)
step 3 delete cookies so the button will be disabled again and proceed with the step 2
this is an "environment" to prove you the function can work.
if you really need echo $element; part then drop the return ... and replace it ..
good luck
<?php
function search_results($u_country, $user_name, $business_user, $brand_name, $product_name, $up_condition, $up_commentary, $up_price, $up_shipping, $up_amount, $up_id,$onoff) {
$onoffanswer=($onoff?'disabled':'');
return <<<stuff
<div>
<form action='search.php' method='post'>
<div class='search_body'>
<span class='country'>$u_country</span>
<span class='username'>$user_name</span>
<span class='business_user'>$business_user</span>
<span class='product_name'>$product_name</span>
<span class='up_condition'>$up_condition</span>
<span class='commentary'>$up_commentary</span>
<span class='price'>$up_price</span>
<span class='amount'>$up_amount</span>
<span>
<input type='number' name='up_amount' min='1' max='$up_amount' placeholder='1'>
</span>
<span>
<input type='hidden' name='product_id' value='$up_id'>
</span>
<span>
<button type='submit' name='add_cart' {$onoffanswer}>Warenkorb</button>
</span>
</div>
</form>
</div>
stuff;
};
session_start();
echo search_results(
'country'
,'username'
,'businessuser'
,'brandname'
,'productname'
,'upcondition'
,'commentary'
,'upprice'
,'upshipping'
,'upamount'
,'1111'
,!isset($_SESSION['username'])
);
print_r ($_SESSION);//phpinfo();
$_SESSION['username']='eeee';
?>
I'm running this locally, the page that features this code has this address:
search.php?page=content
<?php if (isset($_GET['page'])) {
$id=$_GET['page']; if ($id=='content') echo "
<div id='content'>
<form action='search.php?page=generated' method='POST' name='value'>
<input type='text'>
<input type='submit' value='Send'>
</form>
</div>";} else echo "";
?>
I want the text inputted in this form to be sent to
search.php?page=generated
I thought the right way to do this was:
#$temp=$_POST['value'];
echo $temp;
But nothing ever gets sent to 'value'. What am I doing wrong?
I am taking values form database in my php project. The values are taken and displayed fine. The problem is I have a button with each value. And I need a separate link for each button to see which data is selected. So that the functioning would depend on selected data. But each button gets the same name. This is my code.
<?php foreach($query as $row): ?>
<hr id="line">
<div id="wrong">
<!--<button id="connect" type="submit">Connect</button>-->
<span id="name">
<?php
if($row->fname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->fname; ?> </span>
<?php
}
else {
echo $row->fname;
$this->session->set_userdata('re_use',$row->fname);
}
?>
 
<?php
if($row->lname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->lname; ?> </span>
<?php
}
else {
echo $row->lname;
}
?>
</span>
<!-- <select name="back_up">
<option selected><?php echo $row->userid ?></option>
<option>a</option>
</select>-->
<button id="connect" type="submit" >Connect</button>
</div>
<?php endforeach; ?>
Please Help.
Keep a hidden input and set the value to it on click of the button. You can store the value to the radio button and use it inside the JS function.
function setSelected(elm)
{
document.getElementById('inputID').value = elm.value;
}
Outside the foreach loop.
<input type='hidden' name='SelectedUser' id='inputID' />
Inside loop, in the generated code for button.
<button id="connect" onclick='setSelected(this)' type="submit" value="<?php echo $row->userid; ?>">Connect</button>
In server side you can get the value using $_POST['SelectedUser'] when the page is submiited after selecting the user.
How do you create a form in PHP where the results entered in from the user will be generated in a table? When I test the pages, I only get the start of the table, key and results(values) fields but no values!
Here is my code (I'm shortening the number of fields to two since forms can be very long:
Order form:
<?php
foreach ($_POST as $key => $entry)
{
$totalfields++;
echo "<tr>\n";
echo "<td>" . $key . "</td>";
if (is_array ($entry)) {
$count = count($entry);
echo "<td>";
for ($i=0; $i<$count; $i++){
echo $entry[$i] . "<br />";
}
echo "</td>";
} else
{
echo "<td>$entry</td>";
}
echo "<tr>\n";
}
?>
<form method="post" action="address to the results form page">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname"/><br/>
<br/>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname"/><br/>
<br/>
<p><input type="submit" value="Submit"/></p>
</form>
Results form page:
<div id="results table">
<table border='1'>
<tr>
<th>
Field
</th>
<th>
Results
</th>
</tr>
</table>
</div>
This actually posts fine for me (I used PHP_SELF for the form action). I would however suggest that you use $_SERVER[REQUEST_METHOD] to check for POST:
if( $_SERVER[REQUEST_METHOD] == 'POST' ) { ...
In addition, be explicit about calling the posted variables ($_POST[fname] & $_POST[lnane])
Your PHP generating the table should be in the resulting page, not in the form page. Or else, you can point the action of the form to the same page, to get the results posted and processed on the same page.
EDIT: What I mean is this: PHP — as you may know — is processed server-side. It means that your browser sends a request to the server, the server processes the PHP file and then it serve the client the processed version.
The problem in your code is that you have the PHP script processing the $_POST on the page in which the user is supposed to fill up the form. When a user fills up the form and hit submit, he will get directed to the action page, where the $_POST will be available and processed. Your misunderstanding is in the fact that you expect the $_POST to be available on the page where it's posted, and then to send it automagically to the result page. This is not what happens because the server doesn't have an opportunity to catch the information you sent on the same page, because it has already been served…
…unless you make the form action direct to the same page. Then the server catches the information and processes it.
You could go solving this problems in two ways really.
Point your action to the result page as you are doing now, but move your processing code to the resulting page.
Point your action to the same page.
You may even want to do something fancy, like check if there is $_POST information and, if there isn't, show a form. If there is, show the result. This is not at all difficult to implement as it follows:
<?php
if( $_POST ): ?>
<div id="results table">
<table border='1'>
<thead>
<tr>
<th>
Field
</th>
<th>
Results
</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_POST as $key => $entry)
{
echo "<tr>\n";
echo "<td>" . $key . "</td>";
if (is_array ($entry)) {
$count = count($entry);
echo "<td>";
for ($i=0; $i<$count; $i++){
echo $entry[$i] . "<br />";
}
echo "</td>";
} else
{
echo "<td>$entry</td>";
}
echo "<tr>\n";
}
?>
</tbody>
</table>
</div>
<?php else: ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname"/><br/>
<br/>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname"/><br/>
<br/>
<p><input type="submit" value="Submit"/></p>
</form>
<?php endif; ?>
I made a working example for you. Just go to the link and click the Run [F9] button over the top to see it in action:
Working example
I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)