I am having an issue with a php variable not including rows that contain spaces. I have it so the user can easily delete something from their list by clicking the x button on the page. The x button contains the link used to delete the item. The issue is the php variable "Item" seems to not fully include items with spaces. If a user click to delete Car Oil then url would become "http://www.example.com/delete.php/?Item=Car". It does not add the space with the rest of the word to allow it to be deleted from the list. Also if I echo the variable $Item it fully has everything so it would it would display Car Oil. Any help would be loved.
$query = mysql_query("select Items, Loc from Members where Username = '$Username' and Session = '$Session'ORDER BY Loc+0 ASC, Items ASC;");
while ($row = mysql_fetch_array($query)) {
$Item = $row['Items'];
echo "<p1>";
echo $row['Items'], ' - Aisle ' .$row['Loc'];
echo "<a href=http://www.example.com/delete.php/?Item=$Item><img src=http://exmample.com/Images/x.png style='margin-bottom:-5px;'></a>";
echo "</p1>";
}
You might need to urlencode the value on your html. For instance
<a href="http://www.example.com/delete.php/?Item=<?php echo urlencode($itemName); ?>">
click here to delete
</a>
Space is not a valid character in a url
Look at urlencode
echo "<img src=http://exmample.com/Images/x.png style='margin-bottom:-5px;'>";
Related
I have been trying to get my head around how to approach this problem. I am writing a web page book library with the categories ('nodes') as MySQL records. I want to print the list of categories at each level, starting at the highest level, and then allow the user to select a category to travel deeper into the library. The PHP codes runs a saved procedure in MySQL:
//loop the result set
while ($row = mysqli_fetch_array($result)) {
if ($row[1] <> 0) {
echo $row[0];
echo "<br />";
} else { // the first row is just the heading
"</strong>Category: ";
echo $row[0];
echo " : <br />";
}}
Because there are only two test categories, this produces output:
Books :
Nature
Children's Books
However, I want to be able to create an onclick event over 'Nature' and 'Children's Books' so the user can select a category and drill down t the next level via a php function. I can convert the php output into html eg:
<?= "<p>{$row[0]}</p>" ?>
but I can't see how I can identify the row in an onclick event to pass a parameter to the function. Perhaps I need to have a completely different approach?
Add an onclick attribute to the element that calls a JavaScript function that does what you want.
<?= "<p onclick='someFunc({$row['id']})'>{$row[0]}</p>" ?>
Replace id with the actual name of the column containing the ID of the row in the table. someFunc() can use that ID to look up information in an array or object, or send an AJAX request.
I have PHP code generating a row of buttons for the user to click, with each button corresponding to the grade of a student. The PHP also generates the jquery needed to cause a button click to randomly select a student with that grade:
echo "<div id='targetGradeButtons' class='testing'>Randomly select student with target grade: " ;
foreach ($uniquetarget as $i => $target) {
echo "<button id='target-$target'>$target</button> " ; // Generate button for randomly selecting students with certain target
// Create jquery to handle button clicks
echo "<script>
$('#target-$target').click(function(){
randomName('target', '$target');
});
</script>" ;
}
echo "</div>" ;
This works perfectly for most grades. However, it doesn't work for A* grades. The random name selector part of it works fine when I hard code A* as the grade, so the problem is with the code above failing to successfully select the #target-A* button. I believe this is because * is a special character. How can I get around this?
Using PHP functions to automatically add escape characters (backslashes), seems not to help.
Rather than bind a separate handler to each ID, I suggest you give them all the same class, and bind a single handler to the class. It can then get the ID from the target element.
echo "<div id='targetGradeButtons' class='testing'>Randomly select student with target grade: " ;
foreach ($uniquetarget as $i => $target) {
echo "<button id='target-$target' class='target'>$target</button> " ; // Generate button for randomly selecting students with certain target
}
echo "</div>" ;
// Create jquery to handle button clicks
echo "<script>
$('.target').click(function(){
randomName('target', this.id.split('-')[1]);
});
</script>" ;
You can use the Attribute Equals Selector
$('[id=\"target-$target\"')
You can try using exact id match selector.
$('[id="target-A*"]')
Example : https://jsfiddle.net/DinoMyte/1a6mwb13/3/
So here is my problem.I want the id of the href which is taken dynamically to be printed in the url.
My code of category.php:
CATEGORY.PHP
while($row = mysql_fetch_assoc($result))
{
echo '<a id="' . $row['topic_id'] . '" href="category.php?id=' .
$_SESSION['id'] . '&check1=//what should i put here?">;
}
all i want is for check1 to have the id of each link seperately so if i have 3 links(depends on the database) the 2nd link will have id=2 and i want to print id=2 the next time category.php is loaded.
BUT
Notice that all id are first printed and then someone will choose one of these links.
You're already doing it in other places.
It's called concatenation - All that PHP does is output dynamically generated HTML. This means that within the quotes of your echo construct you can concatenate anywhere.
All you have to do is add it to the URL as you did with ID:
while ($row = mysql_fetch_assoc($result)) {
echo '<a id="'. $row['topic_id'] .'" href="category.php?id='. $_SESSION['id'] .'&check1='. $row['topic_id'] .'"></a>'
}
your $row['topic_id'] should be different with every iteration. the $row variable gets set to a new
This will output the same $row['topic_id'] as the id attribute of the link but it will also append it as a $_GET parameter in your category.php.
If you do not know what gets passed on in the $_GET array you could always do a print_r($_GET) anywhere in the script since $_GET always gets set (even without query params, it'll just be empty).
It could be that it is unclear to me what you're asking, it could also be that you want the actual primary key id field in which case you'll just have to swap out the last bit:
&check1=$row['topic_id']
with whatever your ID field is named, probably something along the lines of:
&check1=$row['id']
where id would be the actual id of the row in the database table.
Ok so basically I've got a product database. members search the database by clicking on categories of their choice.
These categories are spans which all trigger a javascript (ajax) function which calls a php query to find all of the subcategories of the span just clicked.
Those subcategories are outputted in a while loop which creates more spans which also trigger the same js function which will link back to the query.
The code below shows the output of the categories displayed based on the output of the database query.
"<div id=\"div1\">";
while($row = mysql_fetch_array($result))
{
$spans++;
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code structure.php\",
\"nya\")'> $row[1] </span>";
echo "<br />";
}
"</div>";
So let's say the output of this is:
HEALTH
FITNESS
EDUCATION
LEISURE
How could i pass the span names as values for the next query so that whatever span is clicked on gets its appropriate value passed to the database search?
Would i have to somehow assign the contents of each span to a variable and then pass the variable to the javascript function which would then pass it to the query?
Im out of ideas as to how to do that, or if i should do it another way.
Thanks!
Try replacing:
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code structure.php
\", \"nya\")'> $row[1] </span>";
With:
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code
structure.php?value=" . urlencode($row[1]) . "\", \"nya\")'> $row[1] </span>";
You may then retrieve the value in $_GET['value'] after clicking the SPAN.
Note: I added some line breaks in the text for ease of reading, don't forget to remove them.
Thank you so much for being so helpful. Owe you all a thank you. I will be asking more questions in the future. Someone has solved the problem by giving me this code:
echo "" . strval($row['style']) . "" . "";
and it worked beautifully!!!!!!!!! You rock!
I am sorry, I don't know how to post follow up questions, so I keep posting each question as a new question. I've never joined any forum before, so don't know how to follow a thread :( Sorry
I previously asked a question, but didn't post my code, so many kind people (thank you so much) who respanded couldn't help me out.
So I'll post my partial code below.
";
echo "Select an item";
echo "";
}
while($row = mysql_fetch_array($result))
{
echo "$row[style] $row[color]";
}
mysql_close($con);
echo "";
echo "";
echo "Enter your 4 digit postcode";
echo "";
echo "";
echo "Enter quantity";
echo "";
echo "";
echo "";
echo "";
?>
Then to process form, I use $_POST['item'] to find out which item was selected, I get the first word, the rest of the words are missing.
For example, if the dropdown box was populated with the follwoing:
Dressmaker mannequin size 12
Mannequin torso PH-9 in skin color
...
if item selected was "Dressmaker manenquin size 12", $_POST['item'] gives me Dressmaker, the rests are missing.
I spent whole last night and today searching, but have made no progress, please help :(
This still applies from my previous post:
//====== Begin previous post
Hopefully, your MYSQL database has a primary key? If it does, set the value of each <option> to the primary key of the item.
For example:
SQL
id desc
1 "dressmaker thing with mannequin"
2 "dressmaker thing no mannequin"
Form PHP
echo "<option value='".$query['id']."'>".$query['desc']."</option>";
When the form is submitted, re-query the database for the desired description. You'll be doing this re-query anyway to retrieve prices and such, yes?
The reason this is happening is that spaces are discouraged in HTML attributes. You shouldn't have an attribute like value='this attribute is spaced'.
//====== End previous post
Basically, change this line:
while($row = mysql_fetch_array($result))
{
echo "<option value=$row[style]>$row[style] $row[color]</option><br />";
}
to
while($row = mysql_fetch_array($result))
{
echo "<option value='".$row['id']."'>$row['style'] $row['color']</option><br />";
}
and add this in process_form.php to get the description:
$desc = mysql_query("SELECT style FROM products WHERE id='".$_POST['item']."';");
You can also use this to get all other related info from the DB right when you need it.
// Another edit
#Cambraca - right on - I forgot to sanitize the quote.
#Ottoman - Your solution is a temporary fix. I strongly recommend applying an id/primary key system if it's not in place. An ounce of prevention is worth a pound of cure.
That is because in php you get what is in the "value" attribute of the dropdown's options.
You need to do something like this:
Replace
echo "<option value=$row[style]>$row[style] $row[color]</option><br />";
with
echo "<option value=\"$row[style] $row[color]\">$row[style] $row[color]</option><br />";
The problem is your lack of quotes in the option "echo" statement.
Try something like this
while($row = mysql_fetch_array($result))
{
printf('<option value="%s">%s %s</option>',
htmlspecialchars($row['style']),
htmlspecialchars($row['style']),
htmlspecialchars($row['color']));
}
Note also, the <br> element does not belong in the <select>
Edit: Added htmlspecialchars to properly escape any HTML entities that might exist in retrieved strings
If it gives you only the first word, then you forgot to enclose the option value="with quotes". Otherwise show us the constructed HTML source.