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.
CodePudding user response:
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;
}
CodePudding user response:
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';
?>