I have trying to create a php cart and I have a table with a couple of products, when I click on the submit button some javascript is called and then the form is submitted.
When I execute the page and press the button I get a page "productcart.php?productid=1&command=add" and chrome says that there is an internal server error. I believe that there is a jscript or PHP error that is not letting the variables be pulled into the php code block. Can anyone see if there is something else I am doing wrong?
also what does the "?productid=1&command=add" mean? why does the browser return that?
<script type="text/javascript">
function addtocart(prod_id)
{
document.productform.productid.value = prod_id;
document.productform.command.value = 'add';
document.productform.submit();
}
</script>
<?php
include("local-connect.php");
include("productfunctions.php");
if($_REQUEST['command'] == 'add' && $_REQUEST['productid'] > 0)
{
$product_id = $_REQUEST['productid'];
add_to_cart($product_id, 1);
echo 'it worked';
//header("Location:cart.php");
exit();
}
?>
<form name="productform" action="">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
<table id="product_table">
<?php
$query = "SELECT * FROM products";
$result = mysqli_query($dbc,$query) or die ("Error querying database");
while($row = mysqli_fetch_array($result))
{
echo '<tr>
<td><img id="shopping_img" src="' .$row['image'] . '"/></td>
<td><p><strong>' . $row['name'] . '</strong></p>
<p>' . $row['descri'] . '</p>
<p>Price:<strong>$' . $row['price'] . '</strong></p></td>
<td><input type="button" value="Add to Cart" onclick="addtocart(' . $row['id'] . ')" /></td>
</tr>';
}
?>
</table>
</form>
This will fix the "?productid=1&command=add" in the URL and protect from code injection.
I added method="post" and used $_POST I also used intval() to insure an integer is sent to your function.
<script type="text/javascript">
function addtocart(prod_id)
{
document.productform.productid.value = prod_id;
document.productform.command.value = 'add';
document.productform.submit();
}
</script>
<?php
include("local-connect.php");
include("productfunctions.php");
if($_POST['command'] == 'add' && $_POST['productid'] > 0)
{
$product_id = intval($_POST['productid']);
add_to_cart($product_id, 1);
echo 'it worked';
//header("Location:cart.php");
exit();
}
?>
<form name="productform" action="" method="post">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
<table id="product_table">
<?php
$query = "SELECT * FROM products";
$result = mysqli_query($dbc,$query) or die ("Error querying database");
while($row = mysqli_fetch_array($result))
{
echo '<tr>
<td><img id="shopping_img" src="' .$row['image'] . '"/></td>
<td><p><strong>' . $row['name'] . '</strong></p>
<p>' . $row['descri'] . '</p>
<p>Price:<strong>$' . $row['price'] . '</strong></p></td>
<td><input type="button" value="Add to Cart" onclick="addtocart(' . $row['id'] . ')" /></td>
</tr>';
}
?>
</table>
</form>
Related
I'm making something for a stocktake.
I have a form that generates with 4 fields. item_code, item_name, packing, quantity
<form action="goods.php" method="post">
<table>
<!-- Headers -->
<tr>
<td><b>Item Code</b></td>
<td><b>Description</b></td>
<td><b>Packing</b></td>
<td><b>Quantity</b></td>
</tr>
<?php
for ($i = 0; $i <= 50; $i++) {
?>
<tr>
<td>
<INPUT TYPE="TEXT" NAME="item_code[<?php echo $i; ?>]" SIZE="6" VALUE="
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $_POST["item_code"][($i)];
}
?>"></td>
<td><?php
if (!empty($_POST["item_code"][($i)])) {
$result = FetchData($_POST["item_code"][($i)]);
echo $result['category'];
}
?>
</td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['item_name'];
}
?></td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['packing'];
}
?></td>
<td><INPUT TYPE="TEXT" NAME="quantity[<?php echo $i; ?>]" SIZE="5" VALUE="
<?php
if (!empty($_POST["quantity"][($i)])) {
echo $_POST["quantity"][($i)];
} else {
echo "";
}
?>"></td>
A js function for the button
<script>
function fillForm(value) {
document.getElementById('value').innerHTML = value;
}
</script>
and a list that is generated with 3 fields. item_code, item_name, packing
<?php
$dbh = dbh_get();
$sql = 'SELECT * FROM goods as goods(item_code, sort) order by human_sort(goods.item_code)';
$v = array();
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
print '
<tr>
<td class="buttonL" id="<php ' . $r['item_code'] . ' ?>" onclick="fillForm()">' . $r['item_code'] . '</td>
<td>' . $r['item_name'] . '</td>
<td>' . $r['packing'] . '</td>
</tr>' . "\n";
}
dbh_free($dbh);
?>
}
I want to put a button on each list row and when it's clicked it populates the first three fields in the form, leaving quantity to be filled out. Then when another is clicked it populates the next form row etc,. It's working fine manually entering from the list, but the list is nearly 5000 items so it's a hassle to keep searching then scrolling up and entering the values.
I don't see how to do this with PHP so I assume I need a javascript function, which is where I'm lost. Let me know if you need more info.
I have the following code that gets generated in PHP.
<?php
foreach ($quote->lineItems as $lineItem) {
$name = "line_item_" . rand();
$txtValue = "Supplier: " . $lineItem->supplierName . " Model: " . $lineItem->modelName . " Price: " . $lineItem->price . " Quantity: " . $lineItem->quantity;
?>
<table style="width:900px">
<tr>
<td><input type='hidden' name='<?= $name ?> id='<?= $name ?>''
value='<?= $lineItem->id ?>,<?= $lineItem->supplierId ?>,<?= $lineItem->supplierName ?>
,<?= $lineItem->modelId ?>,<?= $lineItem->modelName ?>,<?= $lineItem->price ?>
,<?= $lineItem->quantity ?>'/>
<?= $txtValue ?>
</td>
<td>
<button type='button' onclick="onBtnRemove('<?= $name ?>')" class="btn btn-secondary">
Remove
</button>
</td>
</tr>
</table>
<?php
}
?>
The onBtnRemove function looks like this.
function onBtnRemove(idToRemove) {
const toRemove = $("#" + idToRemove);
toRemove.remove();
}
I cannot figure out why the element does not remove.
I have search everywhere and learn that input tag can have an array name attribute like writing name="mark_delete[ ]" as array PHP post by form. But what I'm struggling of is the javascript part (I only use simple javascript anyway) to work.
I have 3 input name: prize[ ], quantity[ ], total[ ];
I want the total get the prize times the quantity.
The prize is fixed
The quantity is where the user's input
The total is read only.
Using onchange in input tag with array name
The 2 code show below work successfully in PHP:
Sample code 1 (using foreach): *You may skip this code
<?php
//delete_account.php
session_start();
if (isset($_SESSION['login_user'])) {
include("dbconn.php");
if (isset($_POST['delete']) && $_POST['mark_delete'] > 0) {
$mark_delete = $_POST['mark_delete'];
foreach ($mark_delete as $x) {
$sql = "UPDATE account SET status = 0 WHERE id_number = '$x'"; //status change to 0 as inactive instead of deleting the row data
mysqli_query($conn, $sql);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Delete Account</title>
</head>
<body>
<h1>Restaurant Management Information System</h1>
<h3>Mark the accounts that you want to delete and press the delete button...</h3>
<div>
<button>Menu</button>
<button>Add New Item</button>
<button>View Activity Log</button>
<button>Logout</button>
</div>
<div>
<form action="delete_account.php" method="post">
<?php
$sql = "SELECT * FROM account ORDER BY last_name, first_name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<ol>";
while ($row = mysqli_fetch_assoc($result)) {
if ($row['status'] == 1) { //display account with status value of 1 as active
echo "<li>";
echo "<input type='checkbox' name='mark_delete[]' value='" . $row['id_number'] . "' />";
echo $row['last_name'] . ", " . $row['first_name'] . "</li>";
}
}
echo "</ol>";
}
else
echo "No account existed...";
?>
<input type="submit" name="delete" value="Delete" />
</form>
</div>
</body>
</html>
<?php
}
else {
header("Location: home.php");
exit();
}
mysqli_close($conn);
?>
Sample code 2 (using while / mysqli_fetch_assoc()): *You may skip this code
<?php
//items.php
session_start();
if (isset($_SESSION['login_user'])) {
include("dbconn.php");
if (isset($_POST['add_stocks'])) {
$amount_add = $_POST['amount_add'];
$sql = "SELECT item_id, stock FROM items";
$result = mysqli_query($conn, $sql);
if (!empty($amount_add)) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
if (is_int((int)$amount_add[$x])) {
$stock = $row['stock'] + $amount_add[$x];
$sql = "UPDATE items SET stock = '$stock' WHERE item_id = '$row[item_id]'";
mysqli_query($conn, $sql);
}
$x++;
}
}
}
else if (isset($_POST['add_new'])) {
$stock = $_POST['stock'];
if (empty($stock))
$stock = 0;
if (is_int((int)$stock)) {
$sql = "INSERT INTO items (item_name, prize, stock) VALUES ('$_POST[item_name]', '$_POST[prize]', '$stock')";
mysqli_query($conn, $sql);
}
}
else if (isset($_POST['item_delete'])) {
$item_delete = $_POST['item_delete'];
$x = 0;
while (!isset($item_delete[$x]))
$x++;
$sql = "DELETE FROM items WHERE item_id = '$item_delete[$x]'";
mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Items</title>
</head>
<body>
<h1>Restaurant Management Information System</h1>
<h3>Restock some items or add a new one...</h3>
<div>
<button>Menu</button>
<button>View Activity Log</button>
<button>Logout</button>
<button>Delete an Account</button>
</div>
<div>
<form action="items.php" method="post">
<div>
<input type="submit" name="add_stocks" value="Add Stocks" onclick="return confirm('Add new stock. Apply?')" />
</div>
<table border="1">
<?php
$sql = "SELECT * FROM items";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
?>
<tr>
<th>ID</th>
<th>Name</th>
<th>Prize</th>
<th>Stock</th>
<th>Add Stocks</th>
<th>Action</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['item_id'] . "</td>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['prize'] . "</td>";
echo "<td>" . $row['stock'] . "</td>";
echo "<td><input type='text' name='amount_add[]' /></td>";
echo "<td><button type='submit' name='item_delete[]' value='" . $row['item_id'] . "' onclick='return confirm(\"You are about to delete < " . $row['item_name'] . " >. Are you sure?\")' />Delete</button></td>";
echo "</tr>";
}
}
else {
echo "<tr>";
echo "<td>No item existed...</td>";
echo "</tr>";
}
?>
</table>
</form>
<form action="items.php" method="post" id="item_table">
<label>Item Name:</label>
<input type="text" name="item_name" placeholder="Name" required="required" />
<label>Item Prize</label>
<input type="text" name="prize" placeholder="Prize" required="required" />
<label>Stock</label>
<input type="text" name="stock" />
<input type="submit" name="add_new" value="Add New Item" onclick="return confirm('Add new item. Apply?')" />
</form>
</div>
</body>
</html>
<?php
}
else {
header("Location: home.php");
exit();
}
mysqli_close($conn);
?>
The 2 codes above works in PHP posting the < tagname name=" name[ ] >
Now the real code: Image (the Total Prize didn't display, why? Sorry, I'm not really good at javascript)
<?php
session_start();
if (isset($_SESSION['login_user'])) {
include("dbconn.php");
/*{
code here
}*/
?>
<!DOCTYPE html>
<html>
<head>
<title>Order</title>
</head>
<body>
<h1>Restaurant Management Information System</h1>
<h3>Choose an amount of items to stock for order...</h3>
<div>
<button>Add New Item</button>
<button>View Activity Log</button>
<button>Logout</button>
<button>Delete an Account</button>
</div>
<div>
<form action="order.php" method="post">
<table border="1">
<div>
<input type="submit" name="order_items" value="Order" onclick="return confirm('Confirm order?')" />
</div>
<?php
$sql = "SELECT * FROM items";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
?>
<tr>
<th>ID</th>
<th>Name</th>
<th>Prize</th>
<th>Stock</th>
<th>Order Qty</th>
<th>Total Prize</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['item_id'] . "</td>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['prize'] . "<input type='hidden' name='prize[]' value='" . $row['prize'] . "' /></td>";
echo "<td>" . $row['stock'] . "</td>";
echo "<td><input type='text' name='quantity[]' onchange='total_prize()' value='0' /></td>"; //not working!
echo "<td><input type='text' name='total[]' readonly /></td>";
echo "</tr>";
}
}
else
echo "No item to display...";
?>
</table>
</form>
<script>
int x = 0; //initialize var only once
function total_prize() { //this function will be call many times
document.getElementByName("total")[x].value = document.getElementByName("prize")[x].value * document.getElementByName("quantity")[x].value;
x++;
}
</script>
</div>
</body>
</html>
<?php
}
else {
header("Location: home.php");
exit();
}
mysqli_close($conn);
?>
Just focus on:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['item_id'] . "</td>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['prize'] . "<input type='hidden' name='prize[]' value='" . $row['prize'] . "' /></td>";
echo "<td>" . $row['stock'] . "</td>";
echo "<td><input type='text' name='quantity[]' onchange='total_prize()' value='0' /></td>"; //not working!
echo "<td><input type='text' name='total[]' readonly /></td>";
echo "</tr>";
...
<script>
int x = 0; //initialize var only once
function total_prize() { //this function will be call many times
document.getElementByName("total")[x].value = document.getElementByName("prize")[x].value * document.getElementByName("quantity")[x].value;
x++;
}
</script>
Thank you!
In your code, whenever a quantity[] is changed, it'll update only 1 item, means when you change the value on 4th quantity[](which is quantity[3]), it still get prize[0] and quantity[0]'s value, and then put the compute result to total[0], and if you now want to change that, you will end up change the value to total[1] and so on...
The problem is that you only want to change nth elements that you're currently playing with, but a static number x can't help you with that, as it only able to ref to 1 index, and it changes when function calls, it won't have a desired behavior.
Solution:
Give some index information to your total_prize function, like give it the current index.
function total_prize(index) {
var prize = document.getElementsByName('prize[]').item(index);
var quanity = document.getElementsByName('quanity[]').item(index);
var total = document.getElementsByName('total[]').item(index);
// If any of the dom element not exist, do nothing.
if (prize === null || quanity === null || total === null) {
return;
}
total.value = strToNumber(prize.value) * strToNumber(quanity.value);
}
function strToNumber(str) {
var num = parseInt(str, 10);
return isNaN(num) ? 0 : num;
}
<input type="hidden" name="prize[]" value="1"/>
<input type="hidden" name="prize[]" value="2"/>
<input type="hidden" name="prize[]" value="3"/>
<input type="hidden" name="prize[]" value="4"/>
Q1<input type="text" name="quanity[]" onchange="total_prize(0)"/><br/>
Q2<input type="text" name="quanity[]" onchange="total_prize(1)"/><br/>
Q3<input type="text" name="quanity[]" onchange="total_prize(2)"/><br/>
Q4<input type="text" name="quanity[]" onchange="total_prize(3)"/><br/>
T1<input type="text" name="total[]" /><br/>
T2<input type="text" name="total[]" /><br/>
T3<input type="text" name="total[]" /><br/>
T4<input type="text" name="total[]" /><br/>
Or use the same function without any given info, but loop through all elements and update all of their value each time the function is called.
function total_prize() {
var prizes = document.getElementsByName('prize[]');
var quanities = document.getElementsByName('quanity[]');
var totals = document.getElementsByName('total[]');
// Use the length that is shortest.
var length = Math.min(prizes.length, quanities.length, totals.length);
var index, price, quanity;
for (index = 0; index < length; index += 1) {
if (quanities[index].value.length === 0) {
totals[index].value = '';
} else {
price = strToNumber(prizes[index].value);
quanity = strToNumber(quanities[index].value);
totals[index].value = price * quanity;
}
}
}
function strToNumber(str) {
var num = parseInt(str, 10);
return isNaN(num) ? 0 : num;
}
<input type="hidden" name="prize[]" value="1"/>
<input type="hidden" name="prize[]" value="2"/>
<input type="hidden" name="prize[]" value="3"/>
<input type="hidden" name="prize[]" value="4"/>
Q1<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
Q2<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
Q3<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
Q4<input type="text" name="quanity[]" onchange="total_prize()"/><br/>
T1<input type="text" name="total[]" /><br/>
T2<input type="text" name="total[]" /><br/>
T3<input type="text" name="total[]" /><br/>
T4<input type="text" name="total[]" /><br/>
Im trying to get a value of an input into my buttons action url before the button is pushed.
I need to get the value of:
id="qty<?php echo $i;?>"
Into where it says:
<?php echo VALUE OF $qtyforaction; ?>
My Code is:
<?php $i = $_product->getId();?>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />
<?php $qtyforaction = 'qty'.$i; ?>
<?php echo $qtyforaction; ?>
<button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/<?php echo VALUE OF $qtyforaction; ?>')" ><span><span>Order Again</span></span></button>
</form>
Would I have to use javascript to get it into the url string? My javascript is a bit ropey, would something like this work?
<script type="text/javascript">
var something= document.getElementById('<?php echo $qtyforaction;?>');
</script>
UPDATE WITH FULL CODE:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
$product_thumbnail_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
$summaryData = Mage::getModel('review/review_summary')->load($item->getProductId());
echo "<li>";
echo "<div class='previous-name'><p><a style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "'>";
echo $item->getName() . "</a></p></div>";
echo "<div class='previous-image'><a href='" . $_product->getProductUrl() . "'>";
echo "<img src='" . $product_small_image_path . "' />";
echo "</a></div>";
echo "<div class='previous-rating'>";
echo "<p><a style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "#product_tabs_review_tabbed'>Review this beer now</a></p>";
echo $summaryData->getRatingSummary() . '% Would buy again <br/>';
echo "<div class='rating-box' style='float:left;'>";
echo "<div class='rating' style='width:" . $summaryData->getRatingSummary() . "%'></div></div>";
echo "</div>";
/**echo "<div class='previous-button'>";
echo '<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation(\'';
echo $this->helper('checkout/cart')->getAddUrl($_product);
echo '\')"><span><span>Order Again</span></span></button>';
echo "</div>";**/
?>
<?php $i = $_product->getId();?>
<div class='previous-button'>
<form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />
<?php $qtyforaction = 'qty'.$i; ?>
<script type="text/javascript">
var xy = document.getElementsByTagName("input")[0].getAttribute("qty");
var x = "<button type='button' class='addtocartbutton button btn-cart' onclick='setLocation()'><span><span>Order Again</span></span></button>";
document.getElementById("buttonaddcart").innerHTML = x;
</script>
<div id="buttonaddcart">
<button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/')" ><span><span>Order Again</span></span></button>
</div>
</form>
</div>
<?php
echo "<div class='previous-clear'></div>";
echo "</li>";
}
}
}
?>
You need to get the value of the id attribute:
var x = document.getElementsByTagName("input")[0].getAttribute("id");
But how do you plan on using PHP to echo out this variable if PHP is loaded and done with before Javascript even comes into play? You could use Javascript though to display this variable on the page if that's all you needed.
If you had something in your HTML to target, you could do it like this:
document.getElementById("result").innerHTML = x;
But anyways, get the action attribute value...
var y = document.getElementsByTagName("form")[0].getAttribute("action");
Then set the action attribute to this + the qty...
document.getElementsByTagName("form")[0].setAttribute("action", x + y);
That should work for you, if not, try wrapping x + y in additional parentheses...("action", (x + y));
I want the $module_id and $user_name be passed to the url.
The code below does not pass the correct values from the dropdown list to the URL query string. Can you please show me what's missing?
<?php
$root = $_SERVER['HTTP_HOST'];
$fullname = $_POST['user_name'];
$user_name = explode(" ", $fullname);
$module_id = $_POST['e_learning_module'];
?>
<form action="<?php echo home_url() . '/certificate/print-request-certificate.php?id=' . $module_id . '&fname=' . $user_name[0] . '&lname=' . $user_name[1]; ?>" method="post" name="">
<select id="user_name" name="user_name">
<option value="default">Names</option>
<?php
$query = mysql_query('SELECT DISTINCT fname, lname FROM wl_activity_logs ORDER BY fname ASC');
while($row = mysql_fetch_array($query)) {
echo '<option value="' . htmlentities($row['fname'], ENT_QUOTES) . ' ' . htmlentities($row['lname'], ENT_QUOTES) . '">' . htmlentities($row['fname'], ENT_QUOTES) . ' ' . htmlentities($row['lname'], ENT_QUOTES) . '</option>';
}
?>
</select>
<input type="hidden" name="user_name" id="user_name_hidden">
<select id='e_learning_module' name="e_learning_module">
<option value="default">E-Learning Modules</option>
<?php
global $post;
$args = array(
'numberposts' => -1,
'post_type' => 'wpsc-product',
'post_status' => 'publish',
'wpsc_product_category' => 'e-learning-modules',
'order' => 'ASC'
);
$e_learning_modules = get_posts($args);
foreach( $e_learning_modules as $post ) {
setup_postdata($post);
$id = $post->ID;
?>
<option value="<? echo $id; ?>"><?php the_title(); ?></option>
<?php
}
?>
</select>
<input type="hidden" name="e_learning_module" id="e_learning_module_hidden">
<input type="submit" value="Print" name='print' />
</form>
Change
<form action="<?php echo home_url() . '/certificate/print-request-certificate.php?id=' . $module_id . '&fname=' . $user_name[0] . '&lname=' . $user_name[1]; ?>" method="post" name="">
To
<form action="<?php echo home_url() . '/certificate/print-request-certificate.php" method="GET" name="">
Add the following code below form tag
<input type="hidden" name="id" value="<?php echo $module_id?>">
<input type="hidden" name="fname" value="<?php echo $user_name[0]?>">
<input type="hidden" name="lname" value="<?php echo $user_name[1]?>">
You haave to use form methode as 'get' (method="get") as below.
<form action="<?php echo home_url() . '/certificate/print-request-certificate.php?id=' . $module_id . '&fname=' . $user_name[0] . '&lname=' . $user_name[1]; ?>" method="get" name="">
You have 2 fields with same name 'user_name':
<select id="user_name" name="user_name"></select>
and
<input type="hidden" name="user_name" id="user_name_hidden">
You need to change the second one to another name (E.g: user_name_hidden) or change the first one to another name (eg: full_name) then use $fullname = $_POST['full_name'];