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/
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 want to display a catalog view of students with php. To be more precisely I have the web page in which I have used jQuery, Ajax to load some parts of the page. What I want to do is to to display a list of students like a catalog with their information in which every item of the list has included first name ,last name, username etc. Also, this item must contain a button. Once the button is clicked the list view should be dismissed and another view with full details of the students should be loaded in the same area.
#Vainglory07 of course I have tried a lot and did not find out any solution. Ok I am posting my code but it is very basic:
<script type="text/javascript">
$(function(){
$("#$data").bind('click', function(){
window.alert("It works");
}
)}
);
</script>
Here is the code in php:
$data=mysql_query("SELECT * FROM perdoruesi WHERE id>2 ");
while($db_field=mysql_fetch_array($data)){
echo "<tr><td>";
echo $db_field['emri']. " ";
echo $db_field['mbiemri']. " ";
echo $db_field['username']. " ";
echo $db_field['passw']. " ";
$data = $db_field['username'];
echo "<button id = '$data'>".$data."</button>";
echo "</td></tr>";
}
when I click the button I want to execute the script but it does nothing. Many thanks for your help.
To match $('#data') you should output id='data':
echo "<button id='data'>".$data."</button>";
This code: id='$data' gives each button a different id (which is probably not what you want)
I want to get details from one site. That web page is having 3 different select box:
1) Choose Branch
2) Choose Semester
3) Choose Exam Year and then click on show button. This site is using AJAX to show table(output).
I tried with HTML dom parser but don't know the parameters to be passed with that. How to automize such thing which works(submit) with AJAX?
My code is:
<?php
include('simple_html_dom.php');
$html = file_get_html("http://www.abc.com/Engineering-Degree/ExamPapers/ExamPapers.aspx");
foreach($html->find('select[id=Branch]') as $branchSelect)
{
echo $branchSelect;
foreach($html->find('select[id=Semester]') as $semSelect)
{
echo $semSelect;
foreach($html->find('select[id=Exam]') as $examSelect)
{
echo $examSelect;
echo "<input type='submit' value='Show' id='BranchSemesterExamBtn'/>";
}
}
}
?>
Those selects are using get method to retrieve data maybe. Goto you browser console(F12) and change those select options. You may get the links there.
I am trying to build a table using PHP and SQL values. One of those table columns is going to have a numerical input that controls other values in that row. This parameter ranges from 0 to 1 and I'm trying to use Javascript to dynamically update the three columns to the right of it depending on the parameter value.
Here is the PHP commands for the html table:
$i = 1;
while($row = mysql_fetch_array($result)){
Print "<tr>";
Print "<td><input id='toChange[$i]' name='toChange[$i]' type='number' step='0.01'
min='0.01' max='0.99' value='.85' </td>";
Print "<td>".$row['dependentOnParameter1'] . "</td>";
Print "<td>".$row['dependentOnParameter2'] . "</td></tr>";
$i++;
}
I have tried many different tags for the input cell, such as toChange[] and toChange$i (this one as if the number grows with the row number). This table prints correctly, and here is the jQuery function I have tried to implement:
$('#prEff[1]').change(function() {
var test = this;
return;
});
I know how to alter the table once I get this function to correctly call, but the use of an input inside an array seems to be causing trouble. Is there a specific jQuery object or tag that must be used for .change for arrays? Or if I can use the table tag, will the this pointer point to the part of the table that has been altered?
You probably need to escape the brackets if they are part of the selector:
$('#prEff\\[1\\]')
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.