So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
Use htmlspecialchars on $row['item'] before inserting it in your document.
So your "dumbed-down" code should be:
while($row=mysql_fetch_array($list)) {
$item = htmlspecialchars($row['item']);
echo "<tr>";
echo "<td onclick='edit_form(\"" . $item . "\");'>" . $item;
echo "</td></tr>";
}
Try the below line instead of the original in your code and see if it works:
echo "<td onclick='edit_form(\"" . str_replace('"',""e;",$row['item']) . "\");\">" . $row['item'];
And where you would like to display the $item field, just replace it in reverse if you get "e; instead of ' " '. For example:
$qoute_free= str_replace('"e','"',$passed_value);
If you are using it in javascript, the function can be as below:
function edit_form(passed_value)
{
new_value=passed_value.replace(/"e;/g,'"');
}
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(" . json_encode($row['item'], JSON_HEX_APOS) . ");'>" . $row['item'];
echo "</td></tr>";
}
I'm just putting the values back into my <form> (when they click on a <td>) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!
$list is a mysql_query I ran at the top of the document
$item is the title of one of my columns in MySQL
while($row=mysql_fetch_array($list)){
$item = json_encode($row['item']);
$item = str_replace("'","'",$item);
echo "<td onclick='edit_form(" . $item . ");'>" . $row['item'] . "</td>";
}
Now it display's correctly in the <form> when <td> is clicked (by jQuery input.val($item)) and it shows up in the table correctly via $row['item']. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!
Related
If I have a large html markup that gets populated with values from the database and gets echoed containig lots of divs that have classes:
echo "<div>";
echo"<div class='className'> {$_results['value']} </div>";
echo"</div>";
. . .
// large markup incoming
How can I save this in a variable so I can send it back as json ? is it possible to do that ?
This is what I am trying to do:
$html = "echo "<div>";
echo"<div class='className'> {$_results['value']} </div>";
echo"</div>";"
echo json_encode(array('html'=> $html, 'otherValue' => $_results['otehr']);
I just don't know how to save all the html in a variable so I can send it back in an array along with other values that need to be used separately.
Using echo means that you output strings. So, if you don't need to output all strings, then concatenate them into one and assign this final string to a variable, e.g.:
$html = "<div>"
. "<div class='className'>" . $_results['value'] . "</div>"
. "</div>";
echo json_encode(array('html'=> $html, 'otherValue' => $_results['otehr']));
A simple fiddle.
I will give you what I think is a great advice.
Use a template system for this, I will recommend you mustacheJS
It will be a little difficult the first time, but you will gain a better and clear code.
I would like to create a link within a while loop where the anchor link is a record from the query and the href passes a variable from the same query to another page so I only have to create one page that displays information based on the passed variable.
echo "<td>";
echo ''$row['result']''';
echo "</td></tr>";
This link kills my page and return an error.
echo "<td>";
echo ''.$row['result'].'';
echo "</td></tr>";
Should fix your code your quotes are not closed right
Your href string concatenation is incorrect.
It seems like you tried closing your strings and concatenating them halfway through and then stopped. Even with Stack Overflow you can see the string errors.
This is how your href echo should look:
echo '<a href="stats_game.php?idGame=' .$row['idGame'] . '>' . $row['result'] . '</a>';
It works fine for me
echo "<td><a href='stats_game.php?idGame=".$row['idGame']."'>".$row['result']."</a></td>";
I know it's very easy to get the text value of a link using jQuery.
But Isn't it possible to get the text value using only PHP? Please have a look at my code:
echo '<table>'
foreach($array['data']['results']['titles'] as $data) {
$title = $data['title'];
$id= $data['id'];
$url = $data['url'];
echo '<tr>';
echo '<td>' . ''.$title.'' . '</td>';
echo '<td>' . '<a href= ' .$url . ' target="_blank" >IMDb Link</a>' . '</td>';
echo '</tr>';
}
echo '</table>';
This picture shows the output of my code
Suppose the user clicked on the Third movie - Batman: The Animated Series. How do I make my movie.php page look like this -- (image below)
The Link Text (which is $title) should be passed to the movie.php page and also the IMDb Link of the corresponding movie (which is stored in the $url variable)
The Only way I know is using $_SESSION but it won't work in this case as it will only store & pass the last value of the foreach loop
Please help me in this regard. Thanks :)
Considering $id as the row ID for the particular movie from the Database. You can use url encoding over here. Your code must be
echo '<td>' . ''.$title.'' . '</td>';
When clicked your URL will look something like.
movie.php?id=3
On your movie.php file use $id = urldecode($_GET['id']); to get the movie ID and you can fetch the relevant data from the DB again.
Let me know if you have any issues.
Try sending all info with Proper Primary key OR here is runtime solution
echo '<td>' . ''.$title.'' . '</td>';
Don't forget to urlencode the variables
each link should contain the unique filed like $id so your url should be echo '<td>' . ''.$title.'' . '</td>';
You can pass the encoded value of $id so end user cannot guess the value.In movie.php you decode the $id& show information on the basis of $id
I am trying to create a hyperlink from two pieces of text split over two cells in a table row.
I am generating my table using PHP to echo out the results from my database to a table.
When it echo's it generates a hyperlink with GET variables at the end which allow the user to visit a page relevant to that information.
The problem is that I can't seem to generate a hyperlink that will go across those table cells, I have looked around the web and there is nothing that says I cannot do this.
As you can see from the screenshot below I am generating a hyperlink inside one table cell but I want the other table cell to have the same hyperlink.
Code
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']. "</td><td>".$row['company_name']."</a>";
echo "</td>";
echo "</tr>";
}
I have a feeling that I will just have to generate two separate hyperlinks for the table cells.
However I am hoping someone here can prove me wrong and save me a few lines of code.
Thanks :)
Using native hyperlinks, you will have to create separate wrappers for each cell.
However, if you want to use JS for linking and redirecting, you could do something like:
.....
<tr class="clickable" data-href="http://google.com">
<td>cell-1</td>
<td>cell-2</td>
<td>cell-3</td>
</tr>
....
and then:
$(function(){
$('tr.clickable').click(function(){
window.location.href = $(this).attr('data-href');
});
});
Simply work around it with JS:
echo "<tr onclick=\"location.href='manager.php?method=view&id=".$info.";'\">";
If you do not want to break the table structure (ie. putting the name and the company into one (multi-column) cell), there is IMHO no way other than generating two hyperlinks.
What you might want to do is to use some CSS for a hover effect and some JavaScript to register a user clicked on a cell (which you can, given the structure above, associate with the tr element).
You can not do it like this. Try instead:
while ($row = $db->fetch_assoc($newest))
{
$url = "manager.php?method=view&id=".$row['id'];
echo "<tr>";
echo "<td>" . $row['first_name']." ". $row['second_name']. "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "</tr>";
}
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']."</a></td><a href='manager.php?method=view&id=".$row['id']."'>".$row['company_name']."</a><td></td>";
echo "</td>";
echo "</tr>";
}
I am trying to make a form that show users name and I am having a little trouble with this part. The ' ' and " " marks dont quite work how they are supposed to. Im trying to echo the options in drop down menu and some how the $wholenames and the last " sign appear in the wrong part of the page. Could someone please tell me what is the correct way of doing this?
Thanks
echo' "<option>'; echo $wholenames; echo'</option>"';
Actually I had looked it wrong it is a little bit more complex. Below you can see the code. The whole dropdown menu does not appear. The wholenames integer appears, but the menu does not...
echo'
<label for="addusertogroup">Add user to an existing group:</label>
<select name="addusertogroup" id="addusertogroup">
'; if(mysql_num_rows($userresult))
{
while($row2 = mysql_fetch_assoc($userresult))
{
$wholename = array("$row2[f_name] $row2[s_name]");
foreach ($wholename as $wholenames) {
echo "<option>$wholenames</option>";
}
}
}
else {
echo "<option>No Names Present</option>";
}
To make it work, simply do this:
echo "<option>";
echo $wholenames;
echo "<option>";
or this:
echo "<option>$wholenames</option>";
or this:
echo '<option>'.$wholenames.'</option>';
All will work, just up to you which one you pick.
You shouldn't have " before <option> and after </option>
It should be something like
echo "<option>". echo $wholenames; echo "</option>";
Also, if $wholenames is an array, you'd better iterate over it:
foreach ($wholenames as $name){
echo "<option>". echo $name; echo "</option>";
}
Any text for options in a HTML SELECT box are written inside the tag. If you don't put your text between the <option> tags the browser will try to insert it to the select box's DOM.
So you could change your code to this:
echo '<option value="myvalue">"' . $wholenames . '"</option>';
Haven't actually tested this code.
Update if the Quotation mark was not meant to be in the output you would simply need to write:
echo '<option value="myvalue">' . $wholenames . '</option>';
In php you can use both " " and ' ' with strings.