How to simulate button click - html

This is my pagination program :
if(isset($_POST['view']))
{
$per_page = 20;
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$start = $per_page * $page;
$start = $start - $per_page;
if(isset($_GET['seller']) ) {
$seller = $_GET['seller'];
} else {
$seller = ($_POST['seller']);
}
echo $seller."_".$start;
$query = "SELECT Kala.mark ,Kala.sharhe_kala, Kala_forooshande.date From Kala Join Kala_forooshande ON Kala.id_kala=Kala_forooshande.id_kala WHERE Kala_forooshande.id_forooshande=(SELECT id_forooshande From Forooshande where forooshande='". $seller ."') LIMIT $start , $per_page";
$result = mysqli_query($db,$query);
$query2 = "SELECT COUNT(*) as total FROM Kala Join Kala_forooshande ON Kala.id_kala=Kala_forooshande.id_kala WHERE Kala_forooshande.id_forooshande=(SELECT id_forooshande From Forooshande where forooshande='". $seller ."')";
$result2 = mysqli_query($db,$query2);
if ($result->num_rows > 0) {
echo " <form method='post' enctype='multipart/form-data'>
<table class='blueTable'><thead><tr><th>شماره</th><th>شرح کالا</th><th>مارک</th><th>تاریخ</th></tr></thead>";
while ($row = $result->fetch_assoc()) {
$tarikh=gregorian_to_jalali((substr($row["date"],0,4)),(substr($row["date"],5,2)),(substr($row["date"],8,2)));
$j=$start++;
echo "<tr><td>" . $j . "</td><td>" . $row["sharhe_kala"] . "</td><td>" . $row["mark"] . "</td><td>" .$tarikh[0]."-".$tarikh[1]."-".$tarikh[2] . "</td></tr>";
}
echo "</table></form> ";
$total = mysqli_fetch_assoc($result2);
$total_page = (ceil($total['total'] / $per_page));
echo "<table class='pagination'>";
$prev = $page - 1;
if ($page <= 1) {
echo "
<td> << </td>
";
} else {
echo "
<td> << </td>
";
}
for ($i = 1; $i <= $total_page; $i++) {
if ($i == $page) {
echo "
<td class='active'>$i</td>";
} else {
echo "
<td>" . $i . "</td>";
}
}
$next = $page + 1;
if ($page >= $total_page) {
echo "
<td>>></td>
";
} else {
echo "
<td> >></td>
";
}
echo "</table>";
}
}
The problem is when the user click on page 2 , the <a> tag sends <a href=\"?page=" . $i . "&seller=".$seller."\">" to see the page number 2 , but it doesn't work because of the if clauseif(isset($_POST['view']) at the begining
So it just works for first page which the view button is clicked .
Is it possible to send button value in the link to simulate on button click event ?

Based on your comment on the question:
i want to be shown only if user click on view button
Then you want more view buttons. Replace your links with their own forms which post the values your server-side code expects. So instead of this:
echo "
<td>" . $i . "</td>";
You might have something like this:
echo "
<td>
<form method=\"post\">
<input type=\"hidden\" name=\"page\" value=\"" . $i "\" />
<input type=\"hidden\" name=\"seller\" value=\"" . $seller "\" />
<input type=\"submit\" name=\"view\" value=\"" . $i "\" />
</form>
</td>";
You can use CSS to style your buttons to look like links if you prefer. Tools like Boostrap make that very easy, just adding classes like "btn btn-link" to any clickable element for example.
But ultimately if you want your links to submit a form them make them forms.

Related

PHP mysql Query Bug

I am trying to load articles from 1 to 5 on the homepage of this website.
When loading the homepage, only results from ID 2 to 5 vs. 1 to 5 are getting displayed and I'm not sure why. It seems there probably is a problem with my while loop but I can't seem to figure it out.
I have added a link to a screenshot of the database to show that there is in fact an article with the ID 1 and I've also added a link to the website itself.
Database in question
Website in question
<div class="bodymainwrap">
<div class="contentwrap">
<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
$page = "between " . (($_GET['id']*5)-5) . " and " . ($_GET['id']*5);
} else {
$page = "between 1 and 5";
}
require_once '/db.connect';
$query = "SELECT * FROM Articles where id " . $page;
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result)
?>
<div class="sidebar" align="center">
<a href="/nothing/index.php?id=2" >Page 2</a>
<?php echo $query ?>
</div>
<?php
while ($row = mysqli_fetch_array($result)){
echo '<div class="entry">';
echo '<img src="/nothing/images/julie.jpg">';
echo'<H2>';
echo $row['Title'];
echo'</h2>';
echo '<p>';
echo $row['Article'];
echo '</p>';
echo '</div>';
}
?>
</div>
Your issue is here I believe, with the lone mysqli_fetch_array call after you get $result
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result) //delete this
Here you have already fetched the first row. Remove that, and just keep it within the loop. That's the underlying reason for how the while loop works. It keeps advancing while mysqli_fetch_array doesn't return null.

data not uploading properly due to >

Is there a way that i can escape >, I have a combo box which has an option below. I tried replacing > with > but it's still not uploading properly.
<select name="Category3" id="Category3">
<option value="Request |Running > 1 hour">Request |Running > 1 hour</option>
What it should upload is just Request |Running > 1 hour
but what is being uploaded is: Request |Running > 1 hour 1 hour' />
i dont know where its getting the other 1 hour' />
i tried removing the > and it just uploaded Request |Running 1 hour without the excess 1 hour' /> but i need it for consistency
here is the php code that im using to upload to db
<?php
error_reporting(0);
require 'include/DB_Open.php';
$Category3 = mysql_real_escape_string ($_POST['Category3']);
if (isset($ABC))
{
$sql="INSERT into XXX category_2
VALUES ('".$Category3."')";
$result=mysql_query($sql)or die(mysql_error());
}
?>
here is my code for retrieving the data
$sql="SELECT category_2
FROM XXX
WHERE resolved_date BETWEEN '" . $date . "' AND '" . $date1 . "'
ORDER BY resolved_date";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo "$numrow";
}
{
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><strong>Category 3</strong></th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
Whenever you're displaying text that might include special HTML characters, you should use htmlentities() to encode it properly, so these characters don't cause the HTML to be misparsed (or worse, allow script injection).
$cat2 = htmlentities($info['category_2']);
echo "<td align='center'>" . $cat2 . "<input type='hidden' name='category_2[]' value='" . $cat2 . "' /> </td>";
You also were missing the ' after value=. I've added that (and quotes around all the other attributes).
Since you're creating multiple inputs with the same name, you need to give them an array-style name so that the server script can get all the values. $_POST['category_2'] will be an array.

HTML dropdown box with MySQL in form

I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work

How to smash two HTML elements together with multiple tags

View image: http://i.stack.imgur.com/yxE4u.jpg
Although this is in PHP, I'm trying to (with HTML) get two words (which use the same class) to push next to each other without calling the class seperately or using the two words in the same if/if else function. I'm sure it's just a simple HTML element.
<?php
if (something){
echo "<p class='message'>hello</p>";
}
if (somethingelse){
echo "<p class='message'>world</p>";
}
?>
As the comments aren't what I'm after, I'll show what I'm using:
<?php
if (!empty($message)){
echo "<p class='message'>" . $message . "</p>";
}
if (!empty($errors)){
echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
One statement or both at the same time may prove true. Hopefully you can see why I can't wrap it all around an HTML tag.
i think you want to do this
<p class="messasge"> <?php echo $message; ?> </p>
RE-FIXED according to updated specifications :
<?php
if (!empty($message)){
echo "<p class='message'>" . $message;
}
if (!empty($errors)){
if (empty($message) echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
}
if ((!empty($message))||(!empty($errors))) echo "</p>";
?>
if JUST message is true it'll print <p class='message'>MESSAGE</p>
if JUST errors is true, it'll print <p class='message'>ERRORS</p>
if BOTH are true, it'll print <p class='message'>MESSAGEERRORS</p>
I think you probably want some variant on the following code:
<?php
$output = '';
if (something)
$output .= 'hello';
if (somethingelse)
$output .= 'world';
if ($output)
echo "<p class='message'>$output</p>";
?>
For your example, you could replace all of the echos in the first two conditions with appending to $message, then only output if there is something to output. There are numerous options but I think this is the most flexible / elegant.
In your case it can be simplified to,
<?php
$output = $message;
if (!empty($errors))
foreach ($errors as $error)
$output .= " - $error<br />";
if (!empty($output))
echo "<p class='message'>$output</p>";
?>
Maybe the dl list is useful
<?php
if (!empty($message)){
echo "<dl class='message'><dt>" . $message . "</dt>";
}
if (!empty($errors)){
foreach ($errors as $error){
echo "<dd> - " . $error . "</dd>";
}
echo "</dl>";
}
?>
<?php
$message = "";
if (something) $message = "hello";
if (somethingelse) $message += " world";
echo "<p class='message'>".$message."</p>";
?>

Alternating colours on a dynamic table

Does anyone know how to have different colours on alternate rows in a table
You can use jquery to add classes to the rows like this:
<script type="text/javascript">
$(document).ready(function () {
$("table.evenodd tr:not([th]):odd").addClass("odd");
$("table.evenodd tr:not([th]):even").addClass("even");
});
</script>
And then use CSS to style:
.even {background-color: #e6e6e6;}
.odd {background-color: #ffffff;}
http://www.w3.org/Style/Examples/007/evenodd
table.evenodd tr:nth-child(even) { background: #CCC; }
table.evenodd tr:nth-child(odd) { background: #FFF; }
You need to apply a style to every second row. You can do that by adding a counter to your loop, and if that counter-variable % 2 == 0, you add the style to the row in question, like this:
$i = 0;
while ($row = mysql_fetch_array($result)){
$style = "";
if($i%2==0)
{
$style = ' style="background-color: #CCC"';
}
echo "<tr".$style."><td>";
echo "<center>" .$row['name']. "</center>";
echo "</td><td>"
echo "<center>" .$row['age']. "</center>";
echo "</td><td>"
echo "<center>" .$row['car']. "</center>";
echo "</td></tr>"
$i++;
}
Typically I would have an even and odd css class that defined the different colours then in PHP when outputting the <tr> I would apply this to the element. You'd typically use a row counter in your loop and mod2 the counter.
$rows = 0;
while ($row = mysql_fetch_array($result)) {
$rows++;
$even_odd = ($rows % 2) ? 'odd' : 'even' ;
echo "<tr class=$even_odd><td>";
echo "<center>" .$row['name']. "</center>";
echo "</td><td>"
echo "<center>" .$row['age']. "</center>";
echo "</td><td>"
echo "<center>" .$row['car']. "</center>";
echo "</td></tr>"
}