Echoing a table in PHP - javascript

I want to have this table in PHP? how can I create this and use it in Javascript after?
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>checking</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='display:none'>" . $row['meaning'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input name=\"f\" type=\"checkbox\" value=\"\"> ";
echo "</td>";
echo "</tr>";
}
echo "</table>";
I want to use columns that have class=hiding attribute. but in this way it doesn't work and I have an error:
document.getElementsByClassName(...).item(0) is null
document.getElementById('hiding').style.visiblility = 'visible';
I think I should echo table another way but I don't know how?
Here is my Javascript code:
document.getElementsByClassName.item(0).('hiding').style.visiblility = 'visible';

You can not use getElementById to get an element by its classname.
Try something like
document.getElementsByClassName('hiding')[0].style.visibility = 'visible'

change style.visiblility to style.display, and style.display = 'block' for shown

Related

How to send data and run a link with that data with one click

I want to send latitude and longitude data so I can search it up on Google Map. Those data has to be selected from a database but I can't figure how to do it in one click.
Here is my code.
<?php
$con = mysqli_connect("localhost","root","","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['latitude'] . "</td>";
echo "<td>" . $row['longitude'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='./clicktest.php?id=".$row['id']."'>View Location</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
And as you can see, it sends the id value of a row that I want to another page which is
<?php
$con=mysqli_connect("localhost","root","","project");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['id'])) {
$result = "SELECT * FROM table1 WHERE id='".$_GET['id']."'" ;
$show = mysqli_query($con, $result);
while($row = mysqli_fetch_array($show))
{
$latitude = $row['latitude'];
$longitude = $row['longitude'];
echo "<a href='http://www.google.com/maps/place/".$latitude.",".$longitude."'>click here</a>";
}
mysqli_close($con);
}
?>
And finally can run a link to Google Map on this page. But what I want is to click on the 'view location' on the first page and jump to the google map page with data on that row without having to go through another page and another click.
I did some research and maybe this is about AJAX? And are there any ways to do it without using AJAX?(since I never use it before)
Thanks
I think you need to directly link to google maps page and use "target=_blank" attribute on it. for example:
echo 'View Location';
You can add as many parameter to the anchor tag as you like, so add the lat and long like this for example
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['latitude'] . "</td>";
echo "<td>" . $row['longitude'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='./clicktest.php?id=".$row['id']
.'&lat=' . $row['latitude']
. '&long' . $row['longitude']"'>View Location</a></td>";
echo "</tr>";
}

How can create a separate click event for each input button displayed using a while loop

I am listing users from the database in a table. for each user; there is an 'Approve' and 'Reject' button.
I want to put my code in such way that when i click each button for each user,only the approval or the reject event should affect that specific user? My table looks like this:
right now when i click 'approve' on one user, the other user's approval event also gets triggered. here is my approval code:
$sql = "select * from user_table ORDER BY date_registered DESC LIMIT 10";
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)){
$emailadr = $row["email"];
$adresses [] = $emailadr;
echo "<tr>";
echo "<td>";
echo $row["firstname"];
echo "</td>";
echo "<td>";
echo $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["idnumber"];
echo "</td>";
echo "<td>";
echo $row["qualification"];
echo "</td>";
echo "<td>";
echo $row["dofa"];
echo "</td>";
echo "<td>";
echo $row["date_registered"];
echo "</td>";
echo "<td>";
echo '<button name="approve" value='. $row["user_id"].'>Approve</button> <button name="reject" value='. $row["user_id"].'>Reject</button>';
echo "</td>";
echo '</tr>';
}
if(isset($_POST['approve'])){
// Approved user ID is now in $_POST['approve']
mail($adresses[$row["user_id"]],'subect
1','approved','From:email#example.com');
}elseif(isset($_POST['reject'])){
//
}
}
print_r($adresses);
When inserting new users, you normally have a auto increment column that relates to each row or user (generally called ID) you can do it a few ways first thing you need to do is include that ID or value so you know which user to change:
Create your Buttons:
<button name="approve" value="5"> Approve</button>
<button name="reject" value="5"> Reject</button>
You would update the value to your user ID
You can then simply check whether you have Approved or Rejected the user:
if(isset($_POST['approve'])){
// Approved user ID is now in $_POST['approve']
}elseif(isset($_POST['reject'])){
// Rejected user ID is now in $_POST['reject']
}
To get $adresses you need to assign it outside the loop. Also updated $adresses [] to array_push($adresses, $row["email"]); just because of preference but can be changed back if you prefer.
<?php
$adresses = array();
$sql = "select * from user_table ORDER BY date_registered DESC LIMIT 10";
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
array_push($adresses, $row["email"]);
echo "<tr>";
echo "<td>";
echo $row["firstname"];
echo "</td>";
echo "<td>";
echo $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["idnumber"];
echo "</td>";
echo "<td>";
echo $row["qualification"];
echo "</td>";
echo "<td>";
echo $row["dofa"];
echo "</td>";
echo "<td>";
echo $row["date_registered"];
echo "</td>";
echo "<td>";
echo '<button name="approve" value='. $row["user_id"].'>Approve</button> <button name="reject" value='. $row["user_id"].'>Reject</button>';
echo "</td>";
echo '</tr>';
}
if(isset($_POST['approve'])){
mail($adresses[$_POST["approve"]],'subect 1','approved','From:email#example.com');
//update DB to approved
}elseif(isset($_POST['reject'])){
//update DB to rejected
}
}
print_r($adresses);
?>
Yes.. Passing the contentid or some unique identifier will solve this.

value of $_POST is always the last value of my <tr>

I have a problem in which I cannot solve. I have a simple page where I query all Users and list them in a table. When a user clicks on one of the table rows, it should be taken to another page where the user can edit information of the that they picked. The problem is that in my script, the $_POST value is always the value of the last
CODE
<?php
include "conn.php";
$pquery = "SELECT * FROM Patient NATURAL JOIN User ORDER BY LastName;";
$patientQuery = $conn->query($pquery);
if (mysqli_num_rows($patientQuery) == 0)
echo "<p>No patients found.</p>";
else{
while($assoc = $patientQuery->fetch_assoc()){
echo "<tr onclick = 'sub();'>";
echo "<td>";
echo $assoc['UserID'];
echo "<input type = 'hidden' name = 'UserID' value = '". $assoc['UserID'] ."' />";
echo "</td>";
echo "<td>";
echo $assoc['FirstName'];
echo "</td>";
echo "<td>";
echo $assoc['LastName'];
echo "</td>";
echo "</tr>";
}
}
?>
<script>
function sub(){
document.getElementById("edit").submit();
return false;
}
</script>
I've slightly modified your code - this should work:
<?php
include "conn.php";
$pquery = "SELECT * FROM Patient NATURAL JOIN User ORDER BY LastName;";
$patientQuery = $conn->query($pquery);
if (mysqli_num_rows($patientQuery) == 0)
echo "<p>No patients found.</p>";
else{
while($assoc = $patientQuery->fetch_assoc()){
echo "<tr onclick = 'sub(". $assoc['UserID'] .");'>";
echo "<td>";
echo $assoc['UserID'];
echo "</td>";
echo "<td>";
echo $assoc['FirstName'];
echo "</td>";
echo "<td>";
echo $assoc['LastName'];
echo "</td>";
echo "</tr>";
}
}
?>
<script>
function sub(UserID){
document.location.href = 'http://www.yourdomain.com/something.php?UserID='+UserID;
return false;
}
</script>

Javascript not working in AJAX response

Below code is the AJAX response to another page. I have added onclick event to table row and written javascript code to handle it. But javascript code doesn't work.Is this wrong way of coding or there is any problem in code. Suggest me a simple solution
<?php
echo '<script type=\"text/javascript\">
function clicked(){
alert("I am an alert box!");
}
</script>';
$q = $_GET['q'];
include 'db_connect.php';
$sql="SELECT name,address,mobile,email,pan,tan FROM client WHERE name = '$q'";
$sql_bill="SELECT clientname,financialyear,receiptno,amount,ddate,type,chequeno,category FROM billing WHERE clientname = '$q'";
$sql_total="SELECT SUM(amount) AS TotalAmount FROM billing";
$result = mysql_query($sql);
$result_bill = mysql_query($sql_bill);
$result_total = mysql_query($sql_total);
$total= mysql_fetch_array($result_total);
echo "<h4><b>Client details</b></h4><table align='center' border='2'>
<tr>
<th>Name</th>
<th>Address</th>
<th>Mobile</th>
<th>Email</th>
<th>PAN</th>
<th>VAT TIN</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['pan'] . "</td>";
echo "<td>" . $row['tan'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h4><b>Payment received details</b></h4><table align='center' border='2'>
<tr>
<th>Client Name</th>
<th>Financial Year</th>
<th>Receipt No</th>
<th>Date</th>
<th>Type</th>
<th>Chequeno</th>
<th>Category</th>
<th>Amount</th>
</tr>";
while($row = mysql_fetch_array($result_bill))
{
echo "<tr onclick=\"clicked()\">";
echo "<td>" . $row['clientname'] . "</td>";
echo "<td>" . $row['financialyear'] . "</td>";
echo "<td>" . $row['receiptno'] . "</td>";
echo "<td>" . $row['ddate'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['chequeno'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan=7>Total</td>";
echo "<td>".$total['TotalAmount']. "</td>";
echo "</tr>";
echo "</table>";
?>
Try this instead of your script code:
echo <<<EOD
<script type="text/javascript">
function clicked(){
alert("I am an alert box!");
}
</script>
EOD;
Edit 1:
Look in the console (F12), What happens when you click on a tr?
The following fiddle replicates your code and works:
http://jsfiddle.net/Yaj44/
Edit 2:
I think it has something to do with the way you call the data.
Put the function in your main page,
then call the AJAX.
Set innerHTML of specific div with responseData.
If you do it in that order, it might work.
Your way to adding this functionality is very dirty..
If you use JQuery, you can add on the end:
$('tr').click(function() {
alert('Do something on click!');
});
mysql_fetch_array is outdated, use mysqli (or PDO) instead.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();

Adding variables to an array php

I have the following problem:
I made a form in which the customer can add some items they want and when he's done he will press the submit button on the from, bringing him to the confirmation page. Where he should see all the products and next to them the amount of items ordered.
And there is my problem, i placed all the products in an array like so:
$producten = array("Pizza Margherita", "Pizza Funghi", "Pizza Hawai", "Pizza Quatto Stagioni", "Pizza Calzone", "Broodje Shoarma" ,"Broodje Donor", "Durum Doner", "Knoflook Saus", "Whiskey Saus", "Sambal Saus");
and i show it on the page like so:
echo "<table>";
foreach ($producten as $producten){
echo "<tr>";
echo "<td>";
echo $producten;
echo "</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
As you can see the second TD is empty, here is where i need to add the amount of items ordered.
But i don't really know how i should make it so that i can add all the $_POST items which i currently have like these:
$Margherita = $_POST['PizzaMargherita'];
$Fungi = $_POST['PizzaFungi'];
$Hawai = $_POST['PizzaHawai'];
$QuattroStagioni = $_POST['PizzaQuattroStagioni'];
$Calzone = $_POST['PizzaCalzone'];
$Shoarma = $_POST['BroodjeShoarma'];
$Doner = $_POST['BroodjeDoner'];
$Durum = $_POST['DurumDoner'];
$Knoflook = $_POST['KnoflookSaus'];
$Whiskey = $_POST['WhiskeySaus'];
$Sambal = $_POST['SambalSaus'];
i read something about making the input field name like products[ ] or something, but the problem is that i'm already using the names for a piece of javascript code on the previous page which, if i change all the names, disables the whole JS code.
I hope my question is clear for you guys.
But to rephrase it very short:
How do I add variables like $Margherita = $_POST['PizzaMargherita']; in an array and let them print out in the other column in my table.
You need a way to associates posts values (lets say $_POST['PizzaMargherita'] for example) with the name of the pizza.
The easiest way is to have your initial array producten indexed with theses keys:
<?php
$producten = array(
'PizzaMargherita' => "Pizza Margherita",
'PizzaFungi' => "Pizza Funghi",
'PizzaHawai' => "Pizza Hawai",
'PizzaQuattroStagioni' => "Pizza Quatto Stagioni",
'PizzaCalzone' => "Pizza Calzone",
'BroodjeShoarma' => "Broodje Shoarma",
'BroodjeDoner' => "Broodje Donor",
'DurumDoner' => "Durum Doner",
'KnoflookSaus' => "Knoflook Saus",
'WhiskeySaus' => "Whiskey Saus",
'SambalSaus' => "Sambal Saus",
);
foreach ($producten as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "</tr>";
}
This solution does not requires you to write all the lines like $Margherita = $_POST['PizzaMargherita'];
Accessing a specific element of an array can be done via the index. Im not sure if that was your question. I just hope so.
$_producten['PizzaMargherita'] = $_POST['PizzaMargherita'];
This will save you a lot of writing. It saves every POST element into the product array and names each index exactly as it is in $_POST
foreach($_POST as $k => $v) {
$producten[$k] = $v;
}
I would make an associative array, using the key as the item and the value as the amount, like this:
$producten = array("Pizza Margherita"=>"2", "Pizza Funghi"=>"1", "Pizza Hawai"=>"2");
then you can foreach like this:
echo "<table>";
foreach ($producten as $item=>$quantity){
echo "<tr>";
echo "<td>";
echo $item;
echo "</td>";
echo "<td>";
echo $quantity;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Try this:
(This is how I think you mean it)
$producten['PizzaMargherita']['Aantal'] = $Margherita;
echo "<table>";
foreach ($product as $producten){
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $product['Aantal'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
That should do it!
P.S. Krijg er honger van!
I know this is not the answer you want to hear but it is the correct and proper way to do this:
orderpage.html
<form action="summary.php" method="POST">
Margherita
<br />
<input type="text" name="pizzas[Margherita]" value="0" />
<br />
<br />
Fungi
<br />
<input type="text" name="pizzas[Fungi]" value="0" />
<br />
<br />
Hawai
<br />
<input type="text" name="pizzas[Hawai]" value="0" />
</form>
summary.php
<?php
// uncomment the line below when you need to debug
// echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($_POST, true).'</pre></div>';
$total_pizzas = 0;
echo "<table>";
foreach ($_POST['pizzas'] as $producten=>$quantity)
{
echo "<tr>";
echo "<td>";
echo "Pizza $producten";
echo "</td>";
echo "<td>";
echo $quantity;
echo "</td>";
echo "</tr>";
// make sure the user gave you a number before adding to total
$total_pizzas+= (ctype_digit($quantity) ? $quantity : 0);
}
echo "<tr>";
echo "<td>";
echo "Total Pizzas";
echo "</td>";
echo "<td>";
echo $total_pizzas;
echo "</td>";
echo "</tr>";
echo "</table>";
?>
If you need help altering the Javascript please let me know.

Categories

Resources