I have a form on an HTML page which posts a user input to a PHP which executes a SQL statement to get information from a database and output it in JSON. Here is my PHP code to get the JSON data:
<?php
header('Content-type:application/json');
$con = mysqli_connect('localhost','root','','dbname');
if(isset($_POST['submit']))
{
$artist = $_POST['artist'];
$select = mysqli_query($con, "SELECT * FROM tblname WHERE artistName = ". $artist);
$rows = array();
while($row = mysqli_fetch_array($select))
{
$rows[] = $row;
}
echo json_encode($rows);
};
?>
When I submit the form on the HTML page, it goes to the PHP page and I get this result:
[{"0":"6","id":"6","1":"OASIS","artistName":"OASIS","2":"SOME MIGHT SAY","songTitle":"SOME MIGHT SAY","3":"1995-05-06","dateNo1":"1995-05-06"}]
(Code and JSON result have been simplified for this question)
Instead of going to this page, I'd like the data to be displayed nicely on the HTML page so that a user does not see this array. I'm not sure how to do this, any help?
Thank you!
If you want the output formatted nicely, why are you encoding in JSON? JSON, although human-readable, isn't intended to be rendered on-screen as an end product.
I'd recommend looping through the $rows array and building a simple HTML table. You could, of course, wrap the data in any markup you like and even add CSS to style it as you see fit. Here's an example to get you started:
echo "<table>";
foreach($rows as $row) {
foreach($row as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
}
echo "</table>";
If you only want some of the data rendered, you can simply echo those values:
foreach($rows as $row) {
{
echo "Artist:" . $row['artistName'];
echo "<br>";
echo "Song Title:" . $row['songTitle'];
echo "<br>";
echo "Date:" . $row['dateNo1'];
}
There are two ways to solve this problem :
Perform AJAX call from Javascript to PHP Page and parse the success response in to the HTML.
Simply echo the values in PHP Page or add HTML itself in PHP Page as suggested by Tim.
Related
I am having problems implementing my $myimage into my echos for session.My goal is to put the $myimage into the echo but idk how to do it so i am hoping someone who comes across my post does.I have a gallery of photos and at the moment when i click on something it doesnt send the photo i clicked on rather on the last photo i added.
<?php
while ($row = mysqli_fetch_assoc($result)) {
$myimage="".$row['image']."";
$_SESSION['myimage'] = $myimage;
echo "<div class='img-single'>
<a href='images/fpage.php'><img class='i' src='images/".$row['image']."'>
</a><figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
trying to make something look like this
<?php
while ($row = mysqli_fetch_assoc($result)) {
//$myimage="".$row['image']."";
$_SESSION['myimage'] = $myimage;
echo "<div class='img-single'>
<a href='images/fpage.php'><img class='i'
src='images/$myimage="".$row['image']."";'>
</a><figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
Here is my code I need how to add a percentage or number to each value in total field I have tried a lot but nothing works.
<?php
$json=file_get_contents("http://www.upliftinghumanity.net/edd-api/sales/?key=75caa8cb60362c89e6ac2b62730cd516&token=6547d40b82cecb81cb7b0ec928554a9e&number=-1");
$data = json_decode($json);
extract(json_decode($json, true));
if (count($data->sales)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->sales as $idx => $sales)
{
// Output a row
echo "<tr>";
echo "<td>$sales->total</td>";
echo "<td>$sales->total+3 </td>";
echo "<td>$sales->gateway</td>";
echo "<td>$sales->email</td>";
echo "<td>$sales->transaction_id</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>
result image
You can add it before echoing it out as a string for example $somevar = $sales->data+1; echo "blahh $somevar"; or echo "blahh {$somevar}";
#Ezekiel the problem is solved what you suggest. i was missing some basic stuff . Thanks –
Hi you can't perform php arithmetic operations which its been qouted as a string, you can probably use the "{$sales->data+3}" but it is always advisable to do thr calculation outside the string as this is only a pseudo code "{$sales->data+3}" and may not work even if you include the curly braces
I have a PHP snippet that generates a table and fills it, and adds a delete button at the end of each row.
while($row = mysql_fetch_array($result)){
$num=$row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><form action='delete.php' method='post'><button type='submit' value=$num name='deleteId'>delete</button></form></td>";
echo "</tr>";
}
The delete.php file is this one :
<?php
$host="localhost";
$username="root";
$password="";
$db_name="students";
mysql_connect("$host", "$username", "$password");
mysql_select_db("$db_name");
$id = $_POST['deleteId'];
$sql="DELETE FROM students WHERE id='$id'";
$result=mysql_query($sql);
?>
I want to do this using Ajax asynchronously, ie. I don't want my page to refresh. Tried a million ways, yet it fails each time. Thanks in advance.
Instead of using a form, you need to write your own JavaScript to handle the server call. Here's one way to do it. Make your PHP look something like this:
while($row = mysql_fetch_array($result)){
$num=$row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><button onclick="deleteStudent($num)">delete</button></td>";
echo "</tr>";
}
And then have a JS function that looks something like this:
function deleteStudent(studentId) {
$.ajax({
url: "delete.php",
method: 'post',
data: {
deleteId: studentId
}
});
}
Another way:
Firstly, assign a unique ID for each of the delete button.
while($row = mysql_fetch_array($result)){
$num = $row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><button id='delete-" . $row['id'] . "'>delete</button></td>";
echo "</tr>";
}
Then use jQuery:
$('button[id^="delete"]').click(function () {
var id = $(this).attr('id').substr(6);
$.ajax({
type: "POST",
url: "delete.php",
data: {deleteId: id}
});
});
I have a list of items from a database and I am trying to post to a specific entry in that list using javascript to submit a form. I have no idea why it's not working though. Here is my code...
<?php
...
while loop to get results {
echo "<form action='scheduled.php?id=$row[id]' method='post' id='sche'>";
echo "<td onclick=\"javascript:document.getElementById('sche').submit();\">".$row['firstname'];
echo "</td>";
echo "</form>";
}
?>
The weird part is that it WILL post, but it doesn't pull the right 'id'. It will take the first one on the list and post to that 'id'.
my URL reads "...scheduled.php?id="
Because of the loop, you are defining many different forms with the same id "sche". You need to give each element their own id.
<?php
...
while loop to get results {
echo "<form action='scheduled.php?id=$row[id]' method='post' id='sche_$row[id]'>";
echo "<td onclick=\"javascript:document.getElementById('sche_$row[id]').submit();\">".$row['firstname'];
echo "</td>";
echo "</form>";
}
?>
Notice the new sche_$row[id] for "id"
I have a HTML Table that is populated from a mySQL table as seen below:
$sql = "SELECT `ID`,`SaveName`, `CourseNumber`, `FormType`, `POSTString`, `DateModified` FROM `SavedFormsTable` WHERE 1";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Save File Name</th>
<th>Course Number</th>
<th>Date Modified</th>
<th>Load Old Form</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['SaveName'] . "</td>";
echo "<td>" . $row['CourseNumber'] . "</td>";
echo "<td>" . $row['DateModified'] . "</td>";
echo '<td><button type="button" onclick="LoadFormJS('.$row['ID'].');">Load Form!</button>';
echo "</tr>";
}
echo "</table>";
My problem is on the 4th line from the bottom. in the HTML <button>
I am trying to create a button IN THE HTML TABLE that when clicked will call a PHP function. But i have to echo the HTML in order to create the table from the mySQL database.
Is there a clever way of to do this???
The PHP function I need to call:
function LoadFormPHP($ID){
$con=mysqli_connect("","User636626","EasyPassword","pansyc5_SavedForms");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = sprintf("SELECT `POSTString` FROM `SavedFormsTable` WHERE `ID`=%d",$ID);
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
//mysql_close($con);
$_POST = unserialize(base64_decode($row["POSTString"]));
}
The JS function i tried (and failed) to call the PHP function from:
<script>
function LoadFormJS(ID){
alert(ID);
<?php LoadFormPHP(ID) ?>
}
</script>
You cannot mix html (client side) and php (server side) together. For this kind of cases AJAX is a good tool. The onclick can contain a request to a javascript function that fires the request back to your server, and returns the desired HTML.
You just have some syntax issues -
echo '<td><button type="button" onclick="LoadForm(' .$row['ID'] .')">Load Form!</button>';
What you could do to call the PHP function is to surround your button with a form using a hidden input to hold the value of $row['ID']. Clicking on the button would load the proper form at that point and you could ditch the inline JS in favor of a form action.