In my PHP I have a line of JSON:
echo '"jsoncaption":"'.$xxx.' '.$yyy.' <br /> ('.$zzz.')",';
I want to have the text line-height: 110%;, so I do this:
echo '<div style=\'line-height: 110%;\'>';
echo '"jsoncaption":"'.$xxx.' '.$yyy.' <br /> ('.$zzz.')",';
echo '</div>';
But this causes an an error in my JS console!
Unexpected token < in JSON at position 55
Without the <div> everything works, but I want to use line-height.
Someone have an idea?
echo '"jsoncaption":"' . '<div style=\'line-height: 110%;\'>' . $xxx .' '.$yyy.' <br /> ('.$zzz.')</div>",';
What did I do?
This would be a more detailed script:
$content = $xxx.' '.$yyy.' <br /> ('.$zzz.')';
$wrapped_content = '<div style=\'line-height: 110%;\'>' . $content . "</div>";
echo '"jsoncaption":"' . $wrapped_content . '",';
EDIT: Untested of course, maybe some quotation marks are wrong...
Related
I'm not a php developer and I have a basic knowledge in html.
I currently have this code
<div class="page-header">
<h1 class="<?php echo $this->item->backlink ? "backlink" : ""; ?>">
<?php echo $this->item->backlink ? '<a class="backtomap" href="' . $this->item->backlink . '">' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>' : "";
echo trim($this->item->title); ?>
</h1>
</div>
now I want to integrate the history.back function in place of the current
onclick="history.back(-1)"
so that the generated html looks something like
<a class="backtomap" href="#" onclick="history.back(-1)>' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>'
I tried to change the code here and there, but without the right knowledge it doesn't really work.
Can anybody help?
Thank you.
Just Change line
'<a class="backtomap" href="' . $this->item->backlink . '">' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>'
with
'<a class="backtomap" href="#" onclick="history.back(-1)" >' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>'
This is my third attempt to find an answer to this question, every other time I was downvoted for one reason or another. So I try this again. I am attempting to send data from a hidden input within a form via ajax. The hidden input gets its value from a php script. Now I can not seem to pull the hidden input value on the receiving page. Now the form I am sending from is generated and propagated within php as is the ajax that fires to send the form to the other page.
When I attempt to call the info from the form on the receiving page it does not seem to receive the data from the first page. The reason I assume this is I get no errors and it will not display the data but it does fire the echo that is located before the fetch array.
Here is the code for the first page. It works in all aspects with what I am trying to do except for the form sending portion. I am leaving a lot out but the ajax and form portions are in there.
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM recipes";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo"
<script>
$(document).ready(function() {//start document ready
$('#" . $row['id'] ."').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/pages/receivingpage.php',
data: $(\"f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"" . $row['id'] ."\" class=\"front\">
<form id=\"" . $row['recipe'] ."\" name=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>
" . $row['recipe'] ."<br></h2>
<img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
<p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
<p>Numer of Calories: " . $row['calories'] ."</p>
</div>
</div>
</div>
</div>
</div>
</div>";
}
mysqli_close($con);
?>
Here is the receiving page. It loads but only displays the echo. If I remove the WHERE within the SELECT statement it displays all the database results(not what is desired).
<?php
$con=mysqli_connect("localhost","base","password","util");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$r = $_POST["f2"]['recipe'];
$sql = "SELECT * FROM recipes WHERE recipe ='".$r."'";
$result = mysqli_query($con,$sql);
echo " 2 ";
while ($row = mysqli_fetch_array($result)) {
echo " " . $row['recipe'] ." ";
}
mysqli_close($con);
?>
Any help will be much appreciated.
Try posting serialized data using id instead of the name of the form. See below the example code.
data: $(\"#f2\").serialize(),
Hope this will help you.
See below updated working code.
UPDATED ANSWER:
page1.php
</script>
<?php
$rows[0]['id'] = 1;
$rows[0]['recipe'] = "Veg Rec";
$rows[0]['cooking'] = "Hot cooking";
$rows[0]['calories'] = 1000;
$rows[0]['image'] = "image.png";
foreach ($rows as $key => $row) {
# code...
echo"
<div id=\"covera\">
<div id=\"holder\" class=\"holder\">
<div id=\"disp\" class=\"disp\">
<div class=\"click diagonal panel\">
<div id=\"" . $row['id'] ."\" class=\"front\">
<form id2=\"" . $row['recipe'] ."\" id=\"f2\">
<input type=\"hidden\" name=\"recipe\" value=\"" . $row['recipe'] ."\">
<h2>
" . $row['recipe'] ."<br></h2>
<img src=\"http://localhost/img/" . $row['image'] ."\" alt=\"Recipe Image\" style=\"width:150px;height:100px;\">
</form>
</div>
<div class=\"back\">
<div class=\"pad\">
<h2>" . $row['recipe'] ."</h2>
<p>" . $row['id'] ."</p>
<p>" . $row['id'] ."</p>
<p>Number of Servings " . $row['servings'] ."</p>
<p>Appx. Cooking Time: " . $row['cooking'] ." Minutes</p>
<p>Numer of Calories: " . $row['calories'] ."</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {//start document ready
$('#" . $row['id'] ."').click(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'page2.php',
data: $(\"#f2\").serialize(),
success: function(d){
$(\"#content-disp\").html(d);
}
});
});
});//end document ready
</script>
";
}
?>
page2.php
<?php
print_r($_POST);
?>
I have this PHP file:
foreach($allfiles as $file) {
echo '<div class="file" id="file' . $file->id . '">';
echo '<div id="checkbox"><img src="images/delete.png" width="15" height="15"></img></div>';
echo '<div id="filename">'. $file->name . '</div>';
echo '<div id="size">| '. $file->size . ' </div>';
echo '<div id="created">'. $file->created . '</div>';
echo '<div id="download"><img src="images/download.png" width="18" height="18"></img></div>';
echo '</div>';
?>
<script>
var id = <?php echo $file->id; ?>
</script>
<?php
}
and this JS file
$('#file' + id).dblclick(function() {
alert(1);
});
What I'm trying to do is that when div "file" + the file id is clicked, it will alert "1" (or open the file).
The problem is that now it only alerts 1 when I doubleclick the last div I have, while it is supposed to be done on all of them.
Is there a solution like foreach div called file + something do ? when doubleclicked.
Sorry if this was very unclear :D
You can use data-* prefixed custom attribute like
echo '<div class="file" data-id="' . $file->id . '">';
Bind your event using the common class and then fetch data-idusing .data()
$('.file').on('dblclick', function() {
alert($(this).data('id'));
});
This question already has answers here:
PHP echo variable and html together
(10 answers)
Closed 8 years ago.
I want to write html image tag inside php but the below code is not working, Please help.
<?php
....
....
echo '<img src="../admin/upload/'<?=$display_img?>" width="120px" height="120px"/>
.....
?>
You don't need the php tags you just need to break out of the string:
$width = 120;
echo '<img src="../admin/upload/' . $display_img . '" width="' . $width . 'px" height="120px"/>';
you are inserting a php code block <? ?> when you are already in another.
<?=$display_img?> the equal sign doesn't belong here and the code is attached to the php tags, so the compiler may not be able to read it. It was better this way <?php echo( $display_img ) ?>, of course if you weren't already in php.
Echoing is a bit messy for me, I would've exited php first like this.
<?php
....
....
?>
<img src="../admin/upload/<?php echo( $display_img ) ?>" width="120px" height="120px"/>
<?php
.....
?>
or here is your original code corrected:
<?php
....
....
echo '<img src="../admin/upload/' . $display_img . '" width="120px" height="120px" />';
// OR
echo '<img src="../admin/upload/{$display_img}" width="120px" height="120px" />';
.....
?>
I am trying to split my last post into more sections.
I am working with AJAX & Database. The goal is to a have a clickable division (button) in a view. When this div is clicked the AJAX query is called and retrieves 5 movies from my DB and displays them in the view. What I am struggling the most with, is how to style the newly retrieved array, that I am displaying with jquery in the original division.
I am trying to follow this w3schools tutorial. I am trying to rework my original styling according to how it is done in the tutorial. But I do not know how to write all the opening tags with their attributes into the echoes.
So if anyone here would be so kind and could show me how to rewrite at least few of them I would really appreciate it. All the lines starting with the comment <!--fix--> are those I need to rewrite.
EDIT ----------------------------------------------------
The problem is that I have a CI view page. On that page is a division with content. I am trying to replace this content with new content retrieved using the xmlhttp request. The request calls a file that establishes connection with my DB, runs the query, stores the result in an array and then echoes back the result already split and styled using the while loop. Below is the content of the file called by the jquery request.
<?php
$con = mysqli_connect('localhost','root','root','obs2');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"obs2");
$sql="SELECT * FROM user WHERE id > 5";
$result = mysqli_query($con,$sql);
echo "<table>";
while($row = mysqli_fetch_array($result))
{
<a style="display:block" href="<?php echo base_url('core/detail/'.$row->id) ?>">
<div id="suggested" onmouseover="" style="cursor: pointer;">
<div id="info">
<div id="info_center">
<p><b><?php echo $row->name ?></b></p>
</div>
</div>
<div class="mosaic-block fade">
<a href="<?php echo base_url('core/detail/'.$row->id) ?>" class="mosaic-overlay">
<div class="details">
<p><?php echo $row->summary ?></p>
</div>
</a>
<div class="mosaic-backdrop"><img src="http://puu.sh/5DSWj.jpg"></div>
</div>
<div id="info">
<div id="info_center">
<p>Rating: 7.7</p>
</div>
</div>
</div>
</a>
}
echo "</table>";
?>
So from what I understand from the tutorial is that I need to be echoing each of the lines in inside the while loop. And what I do not know is how to rewrite all the tags and the php echoes that are already in there. I hope this makes it more clear.
----------------------------------------------------
original code
<table>
<?php foreach ($pages->result() as $row): ?>
<a style="display:block" href="<?php echo base_url('core/detail/'.$row->id) ?>">
<div id="suggested" onmouseover="" style="cursor: pointer;">
<div id="info">
<div id="info_center">
<p><b><?php echo $row->name ?></b></p>
</div>
</div>
<div class="mosaic-block fade">
<a href="<?php echo base_url('core/detail/'.$row->id) ?>" class="mosaic-overlay">
<div class="details">
<p><?php echo $row->summary ?></p>
</div>
</a>
<div class="mosaic-backdrop"><img src="http://puu.sh/5DSWj.jpg"></div>
</div>
<div id="info">
<div id="info_center">
<p>Rating: 7.7</p>
</div>
</div>
</div>
</a>
<?php endforeach; ?>
</table>
according to this
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
progress
echo "<table>";
while($row = mysqli_fetch_array($result))
{
<!--fix--><a style="display:block" href="<?php echo base_url('core/detail/'.$row->id) ?>">
<!--fix--><div id="suggested" onmouseover="" style="cursor: pointer;">
<!--fix--> <div id="info">
<!--fix--> <div id="info_center">
echo "<p>" . $row['name'] . "</p>";
echo "</div>";
echo "</div>";
<!--fix--> <div class="mosaic-block fade">
<!--fix--> <a href="<?php echo base_url('core/detail/'.$row->id) ?>" class="mosaic-overlay">
<!--fix--> <div class="details">
echo "<p>" . $row['summary'] . "</p>";
echo "</div>";
echo "</a>";
<!--fix--> <div class="mosaic-backdrop"><img src="http://puu.sh/5DSWj.jpg"></div>
echo "</div>";
<!--fix--> <div id="info">
<!--fix--> <div id="info_center">
<!--fix--> <p>Rating: 7.7</p>
echo "</div>";
echo "</div>";
echo "</div>";
echo "</a>";
}
echo "</table>";
Your original code is the CORRECT way to do it. The w3schools version is completely wrong and you should avoid that.
Everyone would be better able to assist you if you try to describe what is NOT working with your original code.
Don't follow the convention used in that tutorial. Not only can't you trust everything W3schools says (they even say that in their disclaimer), but it's just less readable. Especially when you have a propert editor, you can benefit from code highlighting in your HTML. That will be gone if you echo all your html. Apart from that you just clutter your code, and you have to escape every quote. Long story short: don't do that. :)
Regarding the echos of the values inside the html, you can shorten this:
<?php echo base_url('core/detail/'.$row->id) ?>
to this:
<?=base_url('core/detail/'.$row->id) ?>
<?= is just a shorthand. I think it even works when the short opening tag (<?) is disabled, but if you want your code to be independent of that setting you should verify that.
Also, though I prefer the { .. } style of blocks, I think using : ... endforeach is much clearer when combined with HTML. So I wouldn't change that either.
If you do want to change it anyway, you will still need the PHP closing tag, which you have forgotten now. So:
while($row = mysqli_fetch_array($result)): ?>
becomes:
while($row = mysqli_fetch_array($result))
{ ?>
Of course you can put { ?> or only the { on the same line as the while statement, if you like.
[edit]
Regarding your comment: Everything outside PHP tags gets outputted as-is. So the code:
Hello <?php echo 'lovely'; ?> world <?php /* More code without output*/ ?>
results in the phrase:
Hello lovely world
So as you can see, it is often much easier to put large chunks of plain text/html outside of PHP tags, and only briefly open and close PHP tags to echo some variable content.