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.
Related
Whenever I'm going to the designated page using the controller, $row values are lost.
The code below shows how I get to the new page.
while($row = $result->fetch_assoc()) {
$html .= "<tr>";
$html .= "<td><a href='?article=del&".$row["id"]."'><i class='fas fa-trash-alt'></a></i></td>";
$html .= "</tr>";
}
Normally, in my complete table, all the data is shown, including the ID and the names in the db etc. But when I get to the new page, the $row value is lost. For example the del page would be this:
<?php
function del() {
print $row;
}
I would get this error:
Notice: Undefined variable: row in C:\wamp64\www\SMS2\article\del.php on line 3
Instead, the url DOES show the $row["id"] for example:
http://localhost/SMS2/index.php?article=del&15
In this case I clicked on the row which had the 'id' value of 15.
So basically, the $row value does get loaded in the url, which is 15, but is lost in the actual page when I try to print it.
While I can't see the rest of your code, your URL is missing the definition of what 15 is. You need to have a variable in the URL. For example:
http://localhost/SMS2/index.php?article=del&id=15
Instead of
http://localhost/SMS2/index.php?article=del&15
Then in your code you would use something like $_GET['id'] to pull the value of 15 out of the URL.
Change
<a href='?article=del&".$row["id"]."'>
into:
<a href='?article=del&id=".$row["id"]."'>
and on the other page:
<?php
$id = $_GET['id'];
print $id;
?>
I have been doing a tutorial on posting to a database with Ajax and now I have it working with 1 form field, but I would like to learn how to get it to work with 2 form fields.
The form field I have working is named content_txt and it is inserted into add_delete_record(content). The second form is named balance_txt and would also be inserted in the same table as content_txt was.
This is the block of code that I'm trying to figure out how to update with the extra form field. I have added notes in it to explain what I am using each thing for and what I want to do:
if(isset($_POST["content_txt"]) && strlen($_POST["content_txt"])>0)
{ //checks $_POST["content_txt"] is not empty, but I am not sure how to add the other field into this
$contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
//using this to strip stuff from the post, but I am not sure how I should added the other form field to this, should I create another variable line or can I add it to this.
// Insert sanitize string in record and I think I have it correct, except if I need to add a variable following content
if(mysql_query("INSERT INTO add_delete_record(`content`,`balance`) VALUES('".$contentToSave."')"))
{
//This returns the results back to the page, do I need to have a duplicate section for the additional record or will this work like a loop.
$my_id = mysql_insert_id(); //Get ID of last inserted row from MySQL
echo '<li id="item_'.$my_id.'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a></div>';
echo $contentToSave.'</li>';
mysql_close($connecDB); //close db connection
if you use two form just named same field name "content_txt" and post it .
or when you ajax data change it name on ajax field .
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;'>";
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.
I have an onclick event called in a PHP foreach loop for several items whose author name I need to click. Only one name was getting generated, so I know that I must have a unique id for each HTML element. I already have a variable $objkey for a counter used in the loop so I appended it to the id.
<?php
//This is the item that appears in the loop.
//Currently, clicking on each one that is generated only generates the first author name
//The correct author as passed by PHP displays in the loop.
//$closest is the value of the author's name.
echo 'Click name to add <span><button id="closest' . $objkey . '" onClick="setAuthor()">' . $closest . '</button></span><br />';
?>
I have done a print_r('$objkey); and the appropriate value for the counter is getting passed along.
I want the value of this button element $closest to be passed to an input field. I have appended the variable to both the input's id and name elements (not sure if that helps) (UPDATE: There is an input field for each of the authors that needs to get the value):
<?php
echo '<input id="author_name' . $objkey . '" type="text" name="author_name' . $objkey . '" value="' . $author . '" />';
?>
Where I'm stuck is with my function:
<script type="text/javascript">function setAuthor() {
var author = document.getElementById("closest").innerHTML;
window.alert(author);
document.forms["myform"].elements["author_name"].value = author; }</script>
How do I create a variable in the function for the unique ids so that each looped item generates distinct names?
One way to do it is to pass this to the the setAuthor function:
<?php
//This is the item that appears in the loop.
//Currently, clicking on each one that is generated only generates the first author name
//The correct author as passed by PHP displays in the loop.
//$closest is the value of the author's name.
echo "Click name to add <button id='closest_{$objkey}' onClick='setAuthor(this)'>{$closest}</button><br/>";
?>
With this approach, you can access the element within setAuthor:
<script type="text/javascript">
function setAuthor(el) {
var author = el.innerHTML,
id = el.id.replace(/^closest_/, '');
if (console) {
console.log(author);
}
document.forms["myform"].elements["author_name_" + id].value = author;
}
</script>
However, you shouldn't set onclick inline and for each element. You should use an event registration function.