passing string to javascript function then show onClick - php

I looked over some solutions, but I coudn't figure out how to get this solved
here is my php
while($row = mysql_fetch_array($result))
{
echo"<div class='span4'>
<h2>$row[title]</h2>
<p>$row[intro]</p>
<p><a class='btn' onclick=parashow($row['contents'])>View details »</a></p>
</div>";
}
I just want to pass the contents into (a function)
and then this function display on click
on div specific id
looking on the web for several hours couldn't help
some say
add
{}
other /' '/
or
'''$row[contents]'''
stil no help
function parashow(x){
//document.getElementById('allrows').style.display = "none";
}
I don't know how to pass $row['contents']
to function argument
and then
get x
and replace it into another div by id ('something')

Try this
while($row = mysql_fetch_array($result))
{
echo"<div class='span4'>
<h2>$row[title]</h2>
<p>$row[intro]</p>
<p><a class='btn' onclick=parashow('".$row['contents']."')>View details »</a></p>
</div>";
}

It's good to generate HTML without PHP:
<?php while($row = mysql_fetch_array($result)) { ?>
<div class='span4'>
<h2><?php echo $row[title]; ?></h2>
<p><?php echo $row[intro]; ?></p>
<p><a class='btn' onclick="parashow('<?php echo $row['contents']; ?>')">View details »</a></p>
</div>
<?php } ?>

try this
<?php while($row = mysql_fetch_array($result)) : ?>
<div class='span4'>
<h2><?php echo $row[title]; ?></h2>
<p><?php echo $row[intro]; ?></p>
<p>View details »</p>
</div>
<?php endwhile; ?>

Related

Turn this into an if / else statement

When an item is chosen on my site, it opens a details page. This is the top of the details page above the html tags:
<?php require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Master_details = "
SELECT *
FROM Master_List
WHERE Master_List.Master_Id = $recordID
";
$Master_details = mysqli_query($conn, $query_Master_details) or die(mysqli_error());
$row_Master_details = mysqli_fetch_assoc($Master_details);
$totalRows_Master_details = mysqli_num_rows($Master_details);
?>
This is the code that makes the body of the page:
<div class="container2">
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
<?php
mysqli_free_result($Master_details);
?>
<!-- end .container2 --></div>
What I would like to do is create an if/else statement that will look at the Style_ID of the selected item and determine if the number is > 3. If it is, I want it to choose an item that has a Style_Id of 1, 2, or 3 and the same Length as the item chosen and return a random row in the layout above, skip a few lines and then display the information for the selected item in the layout above. Else if it is < or = 3, then I need it to just display as above.
I have tried using:
<?php
If (Style_ID > 3) {
echo 'Test';
}Else {
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
}
?>
<?php
mysqli_free_result($Master_details);
?>
But it doesn't work and has syntax errors. How can I create this if/else statement?
Note: I would appreciate being able to get one setup for all of it, but if not just fixing this part would be a big help right now.
Thanks to #Brad for his responses, I finally got this one figured out. I ended up changing some of my field names and finally figured out where to close the php tags to make this work. Here is what I ended up with:
<div class="container2">
<div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div>
<?php
if ($row_master_details['type_id'] > 3) {
echo "Test";
}else { ?>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php
mysqli_free_result($master_details);
?>
<?php } ?>
<!-- end .container2 --></div>
For starters, PHP is case-sensitive.
If (...) {
...
}Else {
You'll want to use lower-case if and else.
Next, if Style_ID is an attribute of the record, you'll need to access it like you did the others.
if ($row_Master_details['Style_ID'] > 3) {

How to load more comments without refreshing the browser

Am working on comment system, but am stuck here, after loading my PHP on my browser, it shows me exactly 2 comments that I want to see, when I click the link it fetches the other 2 comments as I programmed it on jQuery, but after that the button disappears and I cant load more comments.
Please help!
Here is my code,
<script>
$(document).ready(function() {
var commentCount = 2;
$("button").click(function() {
commentCount = commentCount + 2;
$("#comments").load("2.php", {
commentNewCount: commentCount
});
});
});
</script>
<body>
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
<button>More comments</button>
</body>
and this down here is my load-coments.php (i decided to call it 2.php)
<?php
include 'include/dbconnect.php';
$commentNewCount = $_POST['commentNewCount'];
$sql = "SELECT * FROM comments ORDER BY id LIMIT $commentNewCount";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
What do you mean with "button disappears"?. The load function of jquery replaces the html with the recieved html so maybe your button is in the div you're replacing. It's hard to debug your code because of the lack of indents.

target='_blank' not working in any browser

Below is my code for downloading some files from a webpage.
Everything works fine except target="_blank". This button does nothing even though the link is correct. If I right click and press open in a new tab it works but when i press the button it does nothing.
<?php
while ($row = $result->fetch_assoc()) {
$i+=1;
$name = $row["filename"];
$location = "../uploadedfiles/" . $name;
?>
<div class="row">
<p><hr>Name : <?php echo $row["name"] ?> <br> Contact Details : <?php echo $row["number"] ?><br></p>
</div>
<div class="row">
<p>Date Sent: <?php echo date("F d Y-- H:i:s.", filectime($location)) ?><br></p>
</div>
<div class="row">
<p><p> <?php echo $i ?> ) <?php echo substr($name, 10) ?> <a href="../uploadedfiles/<?php echo $name; ?>" target='_blank'><button type='button' class='btn btn-info'>View / Download </button></a></p></p>
</div>
<?php }
?>
<hr>
<?php
} else {
echo "0 results";
}
$con->close();
The way you have created button within the anchor tag is not one of the best practices to be followed.
Ideally you should do something like this:
View/Download
Hope this helps.

Search results not showing

I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)

I want to fetch data using ajax properly

I want to retrieve data from database using ajax the data is retrieving successfully but it is not showing properly on page. Maybe it is the issue of if else condition.
Here is my code:
<?php
$query = mysql_query("SELECT * FROM cart_polling WHERE country = '".$_REQUEST['id']."' ORDER BY sort_order ASC") or die(mysql_error());
$record = mysql_num_rows($query);
while($getcontry = mysql_fetch_array($query)){
?>
<div class="Question_Table">
<div class="Question_Title">Q<?php echo $getcontry['sort_order']; ?>-<?php echo $getcontry['question']; ?></div>
<!-- ****** Box1 START ****** -->
<?php
if($getcontry['img1']!=""){
?>
<div class="bg">
<?php
if(isset($_SESSION['id'])){
?>
<div class="img"><center>
<a href="javascript:void(0)" class="op">
<img src="images/pollimg/<?php echo $getcontry['img1']; ?>" width="104" height="102" class="middleimg2" onclick="getValue('<?php echo $_REQUEST['id']; ?>','<?php echo $getcontry['option1']; ?>','<?php echo $getcontry['id']; ?>','1','<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>','<?php echo $getcontry['question']; ?>')" /></a>
</center></div>
<?php } ?>
<div class="Vote_button">
<img src="images/vote_button.png" onclick="sendvar();" align="middle">
<img src="images/view_results.png" onClick="showresult(<?php echo $getcontry['id']; ?>)" align="middle">
</div>
<?php
}
</div>
<?php } ?> // here the while loops end.
I want (<div id="txtHint<?php echo $getcontry['id']; ?>"></div>) this in else condition
and the above code in if condition when I click on image viewresult.png a function call which retrieve data from database. I want the data will show in else condition and the above code including loop shows in if condition.
The txtHint($getcontray['id']) is the id of every record whose value changes dynamically with the help of while loop. And txthint is also saves the response of ajax.
If I am using if condition than while loop is not running in else condition and the id can't pass to AJAX response and the correct data will not show, so how can I do that?
in line: <img src="images/pollimg/
'<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>'
missing 2 semicolon.
'<?php echo $getcontry['img1']; ?>','<?php echo $getcontry['categoryname']; ?>'
UPDATE:
And missing ?> php end syntax at the bottom of your file:
<?php
}
//here is the missing tag:
?>
</div>
<?php } ?> // here the while loops end.

Categories