I'm trying to echo a form from within an echo statement in my PHP code, everything works except for the submit button, when I click it, it just takes me to the same page I'm in instead of purchase.php
It works when I copy the form out of PHP and use paste it in plain html but that doesn't serve what I need, why would the action of a form not work in a PHP echo?
I tried using escape characters for the quotations, starting a completely fresh echo statement, seperated it from the others, nothing worked, I keep getting redirected to the same page like it's an empty submit button
if($_POST['SELECTION'] == "sony"){
$data=mysqli_query($conn,"SELECT * FROM products where prod_type = 'sony' ");
while($row=mysqli_fetch_assoc($data)){
echo"
<div class='w3-quarter'>
<img src='".$row['img_link']."' width='200px' height='300px' alt='iphone12' style='width:100%'>
<h3>".$row['prod_name']."</h3>
<p>".$row['prod_desc']."</p>
<p>".$row['prod_price']."</p>
<p>". "<form method='POST' action='purchase.php'> <div id = 'hidden' > <input type ='text' name='ninjaid' value='".$row['prod_id']."' > </div> <input type='submit' name='buy' value='Buy'> </form>"." </p>
";
}
use single quote on the outside and double quotes inside working
fine
echo '
<div class="w3-quarter">
<img src="'.$row['img_link'].'" width="200px" height="300px" alt="iphone12" style="width:100%">
<h3>"'.$row['prod_name'].'"</h3>
<p>"'.$row['prod_desc'].'"</p>
<p>"'.$row['prod_price'].'"</p>
<p><form method="POST" action="purchase.php"> <div id = "hidden" > <input type ="text" name="ninjaid" value="'.$row['prod_id'].'" > </div> <input type="submit" name="buy" value="Buy"> </form></p>';
Use curly braces to enclose variables {$var} instead ."$var". to avoid problems with arrays
if($_POST['SELECTION'] == "sony"){
$data=mysqli_query($conn,"SELECT * FROM products where prod_type = 'sony' ");
while($row=mysqli_fetch_assoc($data)){
echo"
<div class='w3-quarter'>
<img src='{$row['img_link']}' width='200px' height='300px' alt='iphone12' style='width:100%'>
<h3>{$row['prod_name']}</h3>
<p>{$row['prod_desc']}</p>
<p>{$row['prod_price']}</p>
<p><form method='POST' action='purchase.php'> <div id = 'hidden' > <input type ='text' name='ninjaid' value='{$row['prod_id']}' > </div> <input type='submit' name='buy' value='Buy'> </form>"." </p>
";
}
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 have an input box, which searches a database for the inputted text and displays the information on the page where the input box is.
Is it possible for once the user has typed in text in the input box and clicked submit, it redirects them to page.php and displays the results on page.php?
$getUsers = mysql_query("SELECT * FROM Users");
$numUsers = mysql_num_rows($getUsers);
$Online = mysql_query("SELECT * FROM Users WHERE $now < expireTime");
$Online = mysql_num_rows($Online);
echo"
<div class='members-browse-page'>
<div style='width: 876px; height: 28px; margin-bottom: 10px; clear: both;'>
<span class='form-label' style='margin-right: 30px;padding-top:5px;'>
<strong>Search:</strong>
</span>
<span>
<span class='search-bar'>
<form action='' method='POST' style='margin:0;padding:0;'>
<input type='text' name='UserSearchBar' maxlength='100' style='width:400px;'>
<input type='submit' name='UserSearchSubmit' value='Search Users'>
<a href='online.aspx'><b>Online (".$Online.")</b></a>
</form>
</span>
</span>
</div>
Simple Give action to your form
<form action='yourpage.php' method='POST' style='margin:0;padding:0;'>
<form action='page.php' method='POST' style='margin:0;padding:0;'>
Just write page.php in form action. you can get search box value on page.php using $_POST method. like $_POST['UserSearchBar']. Use this post value or write appropriate code for it and display result.
I'm having a trouble finding a solution to this. Here is the case, assume that I have 5 books in the database, i will display them by doing a while then mysql_fetch_array assume that I create a table. This is the plot
echo"
<td>
<form action='bookext.php' method='post'>
<input type='submit' value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
the $row['book_title'] works fine because it must display what is the title of the book in the database. But, how can I get it's unique value which is book_id and send it to bookext.php?
PS: Sorry for my title, I can't pull the right english
First of all your current input needs a name attribute:
"<input type='submit' name="title" value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
Since you're using the POST method this will insure the value is available in the $_POST array, like $_POST['title'].
For the id you can add a hidden input to your form:
"<input type='hidden' name='book_id' value='".$row['book_id']."'>"
This will be available to you in $_POST['book_id'] when you submit.
Just insert hidden field to your form
<input type="hidden" name="POST_NAME" value="YOUR_VALUE_TO_IDENTIFY">
Don't use forms in tables.
<form action='bookext.php' method='post'>
<table>
<tr>
<?php foreach($books AS $book){ ?>
<td>
<button type="submit" name="bookId" value="<?php echo $book['id']; ?>">
Title of the book
</button>
</td>
<?php } ?>
</tr>
</table>
</form>
bookext.php
<?php
$id = $_POST['bookId'];
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.
I'm going to make edit menu in my web. so I direct the page from product into edit page. What I'm confused is how to get the productID from product's page to use in edit page?
Here is my code in product
<?php $query= "SELECT * FROM game";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs)) { ?>
<div class="gameBox">
<div style="margin:5px;">
<?php echo "<image src=\"images/".$data['gameId'].".png\" alt=\"gameImage\" </image>"?>
<div class="cleaner"></div>
<div class="myLabel">Name</div><div>: <?php echo $data['gameName'];?></div>
<div class="myLabel">Developer</div><div>: <?php echo $data['gameDeveloper']; ?></div>
<div class="myLabel">Price</div><div>: $ <?php echo $data['gamePrice']; ?></div>
<br />
<a href="edit.php" <?php $id=$data['gameId'];?>><input type="button" value="Edit"/></a>
<input type="button" value="Delete"/>
</div>
</div>
<?php } ?>
and it's my code in edit page
<?php include("connect.php");
$id[0] = $_REQUEST['id'];
$query = "SELECT * FROM game WHERE gameId=".$id."";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs)) { ?>
<form action="doUpdate.php" method="post">
<?php echo "<image src=\"images/".$id.".png\" alt=\"gameImage\" </image>"?>
<div class="cleaner"></div>
<div class="myLabel">Name</div><div>: <input type="text" value="<?php echo $data['gameName'];?>" id="gameName" name="gameName"/></div>
<div class="myLabel">Developer</div><div>: <input type="text" value="<?php echo $data['gameDeveloper'];?>" id="gameDeveloper" name="gameDeveloper"/></div>
<div class="myLabel">Price</div><div>: <input type="text" value="<?php echo $data['gamePrice'];?>" id="gamePrice" name="gamePrice"/></div>
<br/>
<div id="txtError">
<!--error message here-->
</div>
<input type="submit" value="Submit"/>
<input type="button" value="Cancel"/></span>
<?php } ?>
When I try to access edit page, there's an error it said
"Undefined index:$id[0] = $_REQUEST['id'];"
in edit page.
Could anyone help me?
It looks like you're confusing two methods of passing data between pages, forms and query strings in <a href...>s.
Forms:
Data is in <input>-type elements (or friends) and inside a <form...> tag.
For example
<form action="handler.php">
<input type="text" name="var1" />
<input type="text" name="var2" />
<input type="submit">
</form>
Usually passed via POST and accessed in PHP via $_POST.
For example, the values in the text boxes referenced above would be accessed with something like:
<?php
echo $_POST['var1']; // First text box
echo $_POST['var2']; // Second text box
Links:
Passed as query strings in <a href...>, for example:
Click Me
Usually passed via GET and accessed in PHP via $_GET.
For example, the values in the query string provided above would be accessed with something like
<?php
echo $_GET['var1']; // "foo"
echo $_GET['var2']; // "bar"
So in this case it looks like you're hyperlinking an input button -- which is not the usual way to do things, but you would fix it by changing this:
<a href="edit.php" <?php $id=$data['gameId'];?>><input type="button" value="Edit"/></a>
To, this
<input type="button" value="Edit"/>
And then reference the variable in edit.php as $_GET['id'].
But since you know it's going to be an integer and nothing else, something like:
$id = (int)$_GET['id'];
Is good enough sanitation (at least for that variable).
Lastly, I notice you assign a variable to $id[0] but then reference $id. Assigning a variable to $id[0] is not the same as assigning it to $id, as $id is an array in the former and an integer in the latter. It seems to me that you can just drop the [0] w.r.t. $id in your edit.php
You can pass through the query string
<a href="edit.php?<?php $id=$data['gameId'];?>>
In this case your PHP code will get change to
$id[0] = $_SERVER['QUERY_STRING'];
Add the id as a parameter to your edit url:
<input type="button" value="Edit"/>
also at the top of your edit.php:
$id = $_REQUEST['id'];