onClick event in an echo while loop - javascript

I am trying to have an onClick event to open each instance of the edit link that links to the specific ID in my database so that you can just click to open a new window edit what you have to and not have to browse away from the main page.
I can get get the link to work in a regular href and a target blank, but with the onclick event it doesn't appear to work properly and I need to set the parameters for the window. I have searched around and tried many solutions to other similar problems but I just can't figure out a solution. Is this even possible?
Here is the snippet of code I'm working with.
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['lname'] . '</td>';
echo '<td>' . $row['fname'] . '</td>';
echo '<td>' . $row['activity'] . '</td>';
echo '<td>' . $row['guests'] . '</td>';
echo '<td>' . $row['room'] . '</td>';
echo '<td>Edit</td>';
echo '<td><a href="delete.php?id=' . $row['id'] . '" >Delete</a></td>';
echo "</tr>";
Any assistance on this would be amazing, thanks in advance.

Try this:
echo "<td>Edit</td>";
echo "<td><a href='delete.php?id={$row['id']}' >Delete</a></td>";

Related

Displaying products with a specific category jQuery

I have products that I'm getting through an API and I tried creating a catalog with jQuery. I'm using two different requests to get two different product categories. I created my catalog on multiple pages thanks to the slice() function on jQuery like so :
var $el = $("#wrap > div");
var pageSize = 9;
var page = 1;
$el.slice(0, pageSize).css({display: 'block'});
$el.slice(pageSize, $el.length).css({display: 'none'});
function addSlice(num){
return num + pageSize;
}
function subtractSlice(num){
return num - pageSize;
}
var slice = [0, pageSize];
This is how I am displaying the products :
echo '<div id="wrap" class="wrap">';
foreach ($beds['moreProducts']['productWindow'] as $items)
{
echo '<div class="bed_images" id="bed">';
echo '<img data-enlargeable class="images" src=' . $items['mainImageUrl'] . '?f=xxs' . '/>';
echo '<h2 class="img_title">' . $items['name'] . '</h2>';
echo '<p class="img_price">' . $items['priceNumeral'] . ' ^b ' . '</p>';
echo '<p class="img_description">' . $items['typeName'] . '</p>';
echo '</div>';
}
foreach ($sofas['moreProducts']['productWindow'] as $items)
{
echo '<div class="sofa_images" id="sofa">';
echo '<img data-enlargeable class="images" src=' . $items['mainImageUrl'] . '?f=xxs' . '/>';
echo '<h2 class="img_title">' . $items['name'] . '</h2>';
echo '<p class="img_price">' . $items['priceNumeral'] . ' ^b ' . '</p>';
echo '<p class="img_description">' . $items['typeName'] . '</p>';
echo '</div>';
}
I created a selection selector with two options to display either one or the other. I first of all tried with the .hide method but it only hides the 9 items displayed, i then tried the .remove method which seemed like working but it let me empty pages until i reach the pages on which i had my items (for example i had pages 2-23 that were empty) So i'm kinda desperate to know how can i manage to only dislay the selected category ?

passing variable from while loop when user press link (php)

i am searching for a way to pass variable ($result[1]) to a session variable from while loop within a link. will be glad for some help
while ($result = mysqli_fetch_array($dataset,MYSQLI_NUM)){
echo "<tr>";
echo " <td >", "<a href='Rate_Supplier.php'>" . $result[1] ."
</a>","</td>" ;
echo " <td >" . $result[0] . "</td>" ;
echo " <td >" . $result[2] . "</td>" ;
echo "</tr>\n";
}

How to make a link remember what value it had as an href before transferring to be used in the next page

With research, I was able to come up with a neat way to display information in my database into a php page.
Now my next aim is to link each row to another php page that will recognize what account ID the user has clicked
echo "<table border='1'>
<tr>
<th>Account ID</th>
<th>Account Username</th>
<th>Account Password</th>
<th>Account First Name</th>
<th>Account Middle Name</th>
<th>Account Last Name</th>
<th>Account Phase</th>
<th>Account Block - Lot</th>
<th>Account Subdivision</th>
<th>Account Contact Number</th>
<th>Account Email Address</th>
<th>Account Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['acc_no'] . "</td>";
echo "<td>" . $row['acc_user'] . "</td>";
echo "<td>" . $row['acc_pass'] . "</td>";
echo "<td>" . $row['acc_fname'] . "</td>";
echo "<td>" . $row['acc_mname'] . "</td>";
echo "<td>" . $row['acc_lname'] . "</td>";
echo "<td>" . $row['acc_phase'] . "</td>";
echo "<td>" . $row['acc_blk-lot'] . "</td>";
echo "<td>" . $row['acc_subd'] . "</td>";
echo "<td>" . $row['acc_contact_num'] . "</td>";
echo "<td>" . $row['acc_email_address'] . "</td>";
echo "<td>" . $row['acc_status'] . "</td>";
echo "</tr>";
}
echo "</table>";
I tried this echo "<td><a href='target php page here'>" . $row['acc_no'] . "</td>" but all the links generated will have the same value. I think JQuery might be needed for this or something but unfortunately, this stuff is not yet taught in our school or there might be a better work around for my problem. please help.
I am not sure how to integrate the logic inside a While loop, I mean
as long as the loop is not finished, does this mean the $_GET or
$_POST will loop with it?
while($row = mysql_fetch_array($result))
{
echo "<tr>";
// Step 1.
// Create a custom URL that points to this same script,
// passing the account number as a $_GET parameter.
echo '<td>' . $row['acc_no'] . "</td>";
echo "<td>" . $row['acc_user'] . "</td>";
echo "<td>" . $row['acc_pass'] . "</td>";
echo "<td>" . $row['acc_fname'] . "</td>";
echo "<td>" . $row['acc_mname'] . "</td>";
echo "<td>" . $row['acc_lname'] . "</td>";
echo "<td>" . $row['acc_phase'] . "</td>";
echo "<td>" . $row['acc_blk-lot'] . "</td>";
echo "<td>" . $row['acc_subd'] . "</td>";
echo "<td>" . $row['acc_contact_num'] . "</td>";
echo "<td>" . $row['acc_email_address'] . "</td>";
echo "<td>" . $row['acc_status'] . "</td>";
echo "</tr>";
// Step 2.
// Check if the user has already clicked a one of the URLs we custom made in step 1.
if (isset($_GET['id'] && $_GET['id'] == $row['acc_no']){
// the user has, so let's allow them to change the fields.
// This form appears directly under the record that was clicked.
echo '<form action="commitChangesScript.php" method="post">';
echo '<input type="text" name="fname">';
//repeat for any fields you want to be able the change.
// ...
echo '</form>';
}
}
First create the custom URLs, this can be done without a form by simply creating a link to the location with a query string at the end of it.
On the first page load, isset($_GET['id'] will never be true, because the user presumably hasn't selected an account yet. The first page load create the table as well as all the links for specific users. The URLs are unique because they are created using the account number. For account #1, the URL is pointed at thisScript.php?id=1, for account #987 the URL is thisSciprt.php?id=987.
When the user selects an account number, a new form appears allowing them to change fields. This works because $_GET['id'] will be set to the account number that was selected. We can compare that input with the field in the DB allowing all other rows not to have the form.
This is just an example of what would work. The form included would interrupt the <table> which might look kind of gross.
does this mean the $_GET or $_POST will loop with it?
$_GET and $_POST are defined outside of the while, meaning they will not be repeatedly defined. They are however, within scope inside the while loop as demonstrated in the example I've shown.

Confirmationbox inside a php whileloop

I have a site where I can update my Database. In the same page I also have a tabel with the most importent information in the database. This tabel is generated by a php while loop, and at the end of each row I have a Edit-button and a Delete-button. But I want to have a confirmation to the deletebutton. But I dont knopw how..
This is my code:
mysql_connect("localhost","user","pass") or die (mysql_error());
mysql_select_db("kalender") or die(mysql_error());
$data = mysql_query("SELECT * FROM event where datum > now() ORDER BY `pub` DESC LIMIT 0, 40") or die(mysql_error());
echo '<table>';
echo '<tr>';
echo '<th>Nr</th> <th>V.</th> <th>Tid</th><th>Dag</th><th>Datum</th><th>Ă„ndra</th><th>Ta bort</th>';
echo '</tr><tr>';
while($info = mysql_fetch_array( $data))
{
$eventTime=$info['datum'];
$tid= strtotime($eventTime);
echo '<td width="20px">' .$info['id'] . '</td>';
echo '<td width="20px">' .$info['vecka'] . '</td>';
echo '<td width="40px">' .date("H:i", $tid) . '</td>';
echo '<td width="40px">'.$info['dag'] . '</td>';
echo '<td width="50px">' .date("j M", $tid) . '</td>';
echo '<td width="70px"><button id="checkoutbutton" type="button"><Edit</button></td>';
echo '<td width="70px"><button id="checkoutbutton" type="button">Delete</button></td></tr>';
}
echo '</table>';
Please help me!
You try this.
In PHP Script:
Update this in while loop
...
...
$tempId = $info['id'];
echo "<td width='70px'><a href='javascript:;' onClick='go_edit($tempId);'><input type='button' value='Edit'/></a></td>";
echo "<td width='70px'><a href='javascript:;' onClick='go_del($tempId);'><input type='button' value='Delete'/></a></td>";
...
...
In Javascript:
Add these functions in under php script
<script>
function go_edit(id) {
window.location.href = "http://kalend.mizgalski.se/update.php?id="+id;
}
function go_del(id) {
var choice = confirm('Are you sure want to delete?');
if(choice) {
window.location.href = "http://kalend.mizgalski.se/delete?id="+id;
}
}
</script>
You can do confirmation using javascript on the client side before sending data to the server

Javascript/PHP redirect

Hello fellow programmers!
I'm working on a personal project (mainly to learn php/javascript) and have ran into an issue with redirection when clicking on a link. I have a bit of a strange situation on a tabbed page I've created and I think that may be what is causing my problem.
I'm trying to allow the user to click the (which due to css has made it look different than normal ) to redirect them to a new page with more details. I THINK that the second tag on my page is what is throwing me off because I have a form in it.
I have tried tons of different things like window.location.href="", location.href="", document.location="", etc... But the same thing always occurs. I am able to get both alert messages, so I know I am getting into my JavaScript (even when I put it into it's own .js file).
Anyway advice/help would be very helpful. Also, if anyone has a suggestion on cleaning this code up a bit, that would also be truly helpful.
Below is basically what I have.
Thanks in advance for your help!
<html>
<head>
<title>test site</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">
function viewDetails(modelId){
alert(modelId);
window.location.href="new url?ModelID=" + modelId;
alert('redirecting would be way awesome...');
}
</script>
</head>
<body onload="load()">
<div id="tabbed_box_1" class="tabbed_box">
<h4>Navigation Tabs<small>Select a tab</small></h4>
<div class="tabbed_area">
<?php
mysql_connect('host','user','password');
mysql_select_db("database");
echo "<ul class='tabs'>";
echo "<li><a href='javascript:tabSwitch(1, 2);' id='tab_1' class='active'>Inventory</a></li>";
echo "<li><a href='javascript:tabSwitch(2, 2);' id='tab_2' >Add Project</a></li>";
echo "</ul>";
echo "<div id='content_1' class='content'>";
echo "<ul>";
$modelsSQL = "SELECT * FROM Model ORDER BY Name";
$modelsResult = mysql_query($modelsSQL);
while ($modelRow = mysql_fetch_array($modelsResult)){
$modelID = $modelRow[0];
$sqlAvailCount = "SELECT * FROM Project WHERE ModelID = " . $modelID . " AND Sold = 0";
$sqlSoldCount = "SELECT * FROM Project WHERE ModelID = " . $modelID . " AND Sold = 1";
$resultAvailCount = mysql_query($sqlAvailCount);
$resultSoldCount = mysql_query($sqlSoldCount);
$rowAvailCount = mysql_num_rows($resultAvailCount);
$rowSoldCount = mysql_num_rows($resultSoldCount);
echo "<li><a href='' onclick='javascript:viewDetails($modelID);'>" . $modelRow[1] . "<small>in stock: <value>"
. $rowAvailCount . "</value> sold: <value>" . $rowSoldCount . "</value></small></a></li>";
}
echo "</ul>";
echo "</div>";
echo "<div id='content_2' class='content'>";
echo "<form action='project_insert.php' method='post' name='projectAddForm'>";
echo "<table cellpadding='5'>";
// Project Model Selection
echo "<tr><td>";
echo "<label for='model'>Model</label>";
echo "</td><td>";
echo "<select name='model' style='width: 250px;'>";
echo "<option value='-1' selected>SELECT</option>";
$modelListSQL = "SELECT * FROM Model ORDER BY Name";
$modelListResult = mysql_query($modelListSQL);
while ($modelListRow = mysql_fetch_array($modelListResult)){
echo "<option value='" . $modelListRow['ID'] . "'>" . $modelListRow['Name'] . "</option>";
}
echo "</select>";
echo "</td></tr>";
// Project Material Selection
echo "<tr><td>";
echo "<label for='material'>material</label>";
echo "</td><td>";
echo "<select name='material' style='width: 250px;'>";
echo "<option value='-1' selected>SELECT</option>";
$materialListSQL = "SELECT * FROM Material ORDER BY Name";
$materialListResult = mysql_query($materialListSQL);
while ($materialListRow = mysql_fetch_array($materialListResult)){
echo "<option value='" . $materialListRow['ID'] . "'>" . $materialListRow['Name'] . "</option>";
}
echo "</select>";
echo "</td></tr>";
// Project Finish Selection
echo "<tr><td>";
echo "<label for='finish'>finish</label>";
echo "</td><td>";
echo "<select name='finish' style='width: 250px;'>";
echo "<option value='-1' selected>SELECT</option>";
$finishListSQL = "SELECT * FROM Finish ORDER BY Name";
$finishListResult = mysql_query($finishListSQL);
while ($finishListRow = mysql_fetch_array($finishListResult))
{
echo "<option value='" . $finishListRow['ID'] . "'>" . $finishListRow['Name'] . "</option>";
}
echo "</select>";
echo "</td></tr>";
// Project Craftsman Selection
echo "<tr><td>";
echo "<label for='craftsman'>craftsman</label>";
echo "</td><td>";
echo "<select name='craftsman' style='width: 250px;'>";
echo "<option value='-1' selected>SELECT</option>";
$craftsmanListSQL = "SELECT * FROM Craftsman ORDER BY FirstName";
$craftsmanListResult = mysql_query($craftsmanListSQL);
while ($craftsmanListRow = mysql_fetch_array($craftsmanListResult)){
echo "<option value='" . $craftsmanListRow['ID'] . "'>" . $craftsmanListRow['FirstName'] . " " . $craftsmanListRow['LastName'] . "</option>";
}
echo "</select>";
echo "</td></tr>";
//Project Description
echo "<tr><td>";
echo "<label for='description'>Description</label>";
echo "</td><td>";
echo "<input type='text' name='description' id='textArea' style='width:250px'>";
echo "</td></tr>";
// Project Selling Price
echo "<tr><td>";
echo "<label for='price'>Price</label>";
echo "</td><td>";
echo "<input id='price' name='price' type='number' style='width:150px'>";
echo "</td></tr>";
// Project Completion Date
echo "<tr><td>";
echo "<label for='date'>Finish Date</label>";
echo "</td><td>";
$dateArray = getdate();
$month = $dateArray[mon];
$day = $dateArray[mday];
if ($month < 10){
$month = '0' . $dateArray[mon];
}
if ($day < 10){
$day = '0' . $dateArray[mday];
}
$todaysDate = $dateArray[year] . '-' . $month . '-' . $day;
echo "<input type='date' name='date' value='" . $todaysDate . "' style='width:150px'>";
echo "</td></tr>";
// Buttons
echo "<tr><td align='center'>";
echo "<input type='button' name='Save' value='Save' onclick='javascript:validateAndSubmit(this.form);' style='width:100px'>";
echo "</td><td align='center'>";
echo "<input type='button' name='Cancel' value='Cancel' onclick='javascript:cancelEntry();' style='width:100px'>";
echo "</td></tr>";
echo "</table>";
echo "</form>";
echo "</div>";
?>
</div>
</div>
</body>
window.location.href may not trigger reload in some browsers and cases..
You should add a reload after
like this:
window.location.href = '/foo/bar/';
window.locaton.reload(true)
But, some browsers delay milliseconds to perform location.href set. In this cases the window.location.reload(true) may complete before this.
Therefore, add a timeout in reload:
window.location.href = '/foo/bar/';
setTimeout('window.locaton.reload(true)', 500);
works in all browsers for me
Good morning! I found the cause of my problem this morning and have been able to resolve the issue. The problem I was causing is because I am using the tag (stylized by css) to display the information, with an onclick event to call my JS code to redirect. Within the tag I had the href='', thinking that the JS would override that functionality, it doesn't!
Removing the href='' from the tag resolved the issue and allowed me to redirect to the new page. A second solution is to use my php code to dynamically create the href link within the tag.
echo "<li><a onclick='viewDetails($modelID);'>" . $modelRow[1] . "<small>in stock: <value>" . $rowAvailCount . "</value> sold: <value>" . $rowSoldCount . "</value></small></a></li>";
OR
echo "<li><a href='inventorydetails.php?ModelID=" . $modelID . "'>" . $modelRow[1] . "<small>in stock: <value>" . $rowAvailCount . "</value> sold: <value>" . $rowSoldCount . "</value></small></a></li>";
I think I will go with the second example for two reasons. First, it provides the link icon when hovering (which I know I can add through css, but this is easier. Second, less JS code.
I thank you for all your help in resolving this!

Categories