how to change these lines of javascript from scriptaculous to jQuery syntax - php

I am rewriting a helper class which was originally built on top of Scriptaculous. I am moving to jQuery - however, I am stuck on a couple of lines, that I need help with (see below): Note: the javascript code is interpersed with php variables (the sigils being a dead give away)
Statement 1
'new Insertion.Before(\'' . $updateContainer . '\', new Element(\'div\', {\'id\': \'' . $updateContainer . '_loading\', \'class\': \'' . $spinnerClass .'\'})); $(\'' . $updateContainer . '_loading\').innerHTML="<h4>Loading ...</h4>";',
Statement 2
'$(\'' . $updateContainer . '_loading\').remove();'

I'll assume that the $updateContainer is ID of the HTML element that contains the loading message.
Then, I'd write the Statement 1 like this:
$statement1 = sprintf('$(\'#%1$s\').html(\'<div id="%1$s_loading" class="%2$s"><h4>Loading</h4></div>\');', $updateContainer, $spinnerClass);
And the second statement is:
$statement2 = sprintf('$(\'#%s_loading\').remove();', $updateContainer);
If you have a lot of AJAX communication and need the 'Loading' often, it might be better to hide() it so you can just show() it later instead of creating the HTML again.
Statement1 would be uset to create the loading element, Statement2 with hide() instead of remove() to hide it and Statement3 with show() instead of hide() to show it again.

Related

PHP dropdown with <select> using variables 'id'

I did many HTML dropdown (which generate select tags etc) with PHP scripts for different HTML files. Previously, I had as many dropdown scripts as there are HTML files. But now, I would like to have one script for all HTML files. For this, I need to have a variable 'id' attribut of the select tag which depend of the parameter of the url of each HTML page. I try to affect the parameter of the url to the 'id' attribut but without results.
Here is the url of one of the web page with a parameter 'name'.
http://127.0.0.1/projetwebtest/projetweb/indexSuperClass.php?name=superClass
And here is the code of the PHP script where I try to affect the url parameter value to the 'id' attribut.
One way :
$opt .= "<select class = 'custom-dropdown__select custom-dropdown__select--emerald' id = '<?php echo $_GET['name']; ?>' onchange = 'change();'>";
Another way :
$opt .= "<select class = 'custom-dropdown__select custom-dropdown__select--emerald' id = '$_GET['name'];' onchange = 'change();'>";
When I try to print with a 'echo' the url parameter $_GET['name'] it works but it doesn't in this case.
How can I affect the url paramter to the 'id' attribut ?
Thanks.
Your string is comprised of various uses of " and ' which is causing it to fail. Here's a working example. You can copy and paste it here to test it online.
$_GET['name'] = 'SELECT_TEST_ID';
$opt = '<select class="custom-dropdown__select custom-dropdown__select--emerald" id="' . $_GET['name'] . '" onchange="change();">';
echo $opt;
Sidenote: When you're creating HTML strings like these, and are not using a template engine, try using 'HTML String' instead of "HTML String", letting you avoid quote hell for HTML attributes.

Get array details PHP

I have a google map on this page, all markers were generated by submit postcodes. So I have the array below, loop info of each marker,
imploded as ("array", "array") format, I am trying to click on a infoWindow and display the according marker details on details.php.
The problem is everything is on the button onclick event, only a simple get on the second page.
This is working, but it is a very bad way. Because the limit to URL length and security reasons;
I would like to be able to get an array info from details.php page,
and the button onclick event trigger url looks like: details.php?marker=id
I don't know what is the best way to go about this, can someone pointing me to the right direction please?
index.php
$info = array();
foreach($stmt as $x)
{
$info[] =
"<h4>" . $x['name'] . "</h4><hr />".
"<h5>Address: </h5>" . $x['Address']."<br />" .
"<h5>Postcode: " . $x['postcode'] ."</h5><br />" .
"<button onclick='window.location.href= \\\"details.php?marker=". "<h4>" . $x['name'] . "</h4><hr />".
"<h5>Address: </h5>" . $x['address']."<br />" . "<h5>Postcode: " . $x['postcode'] ."</h5><br />" . "\\\" ' >
View Details</button>";
}
$i=' "'.implode('","', $info).'"';
details.php
echo $infomarker = $_GET['marker'];
You have to use $x['id'] insted of $x['name'] which is unique in your database and also use base64_encode() for encryption of your id "details.php?marker=".base64_encode($x['id'])."
In your details.php
$infomarker = base64_decode($_GET['marker']);
Try using AJAX to get the info from details.php and then load it into your InfoWindow.
I didn't realise how simple this was, all I need is use that id, write it inside a sql statement in details page then call any part of the statement out. Thanks everyone. Thanks to #Manjeet Barnala for encode tips.

Dynamic ID between PHP, SQL and jQuery

I have a SQL database that PHP is getting the contents of. It gets the info from each row of a table and displays it on a web page. There is also an anchor and a hidden div containing more details from that row of the SQL table. When the use clicks the anchor the jQuery needs to detect it and show the hidden div. The problem I have is that each anchor and hidden div have a dynamic id of:
'ev_a_' . $row['id']
'ev_' . $row['id']
the jQuery I assume needs to look something like this:
$('#ev_a_')[].click(function(){
$('#ev_')[].show();
}
I'm not entirely sure how this works and I've never used dynamic ids before in jQuery. Any suggestions?
I should point out the PHP/SQL code is:
while($row = mysql_fetch_array($q_result)){
echo "<br/><b>" . $row['a'] . "</b><br/>" . $row['b'] . ", " . $row['c'] . "<br/>" . $row['d'] . "<br/><a class='ev_event_a'>More Details</a><br/><div class='ev_event'>" . $row['e'] . "</div>";
}
I would use a class instead for these links instead and set the ID in a data attribute. Depending on your needs you don't even need the ID as you can show the next element if it is already part of the DOM.
A simple example (ID not used...):
Toggle details
<div class="initially_hidden">
...
</div>
....
Toggle details
<div class="initially_hidden">
...
</div>
....
and the javascript:
$('.show_more').on('click', function(e) {
e.preventDefault();
$(this).next().slideToggle();
// if you need the ID, you can get it via $(this).data('id')
});

PHP echo button onclick function

For a little "webshop project" I create a table with PHP and echo"..." function. The table displays some values and in the last cells, there shall be a button which enables the user to delete the corresponding row (or better said, purchase). The data is held in a database and read out while the page loads and than displayed in the table.
I use a "purchase id" to find out which rows have to be deleted, and it works fine if I just implement the function itself. The problem is that I can't get the function working as "onclick" event for the button.
So, some code:
function delete_purchase($purchase_id){
mysql_query("DELETE FROM purchase WHERE purch_id = '$purchase_id'");};
That's the PHP function which deletes the rows, easy enough.
$result = mysql_query("SELECT purchase.purch_id, item.name, purchase.amount, purchase.purch_date, delivery.meaning, item.weight FROM purchase, item, delivery WHERE purchase.cust_id='$cust_id' AND delivery.del_id = purchase.delivered AND purchase.item_id = item.item_id");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" . $row['weight'] * $row['amount'] . "</td>";
echo "<td>" . $row['purch_date'] . "</td>";
echo "<td>" . $row['meaning'] . "</td>";
echo "<td><button onclick=\"delete_purchase('" . $row['purch_id'] . "')\">Kill</button></td>";
echo "</tr>";
}
And this is the part which doesn't seem to work. I get the variable and some other values from the database and insert them into my table as long as there are values. Everything is displayed, even the buttons; but clicking on them doesn't do anything.
Source code of the website seems fine:
<td><button onclick="delete_purchase('138')">Kill</button></td>
Hope everything's clear, and you guys have some ideas what's wrong. If you need to know additional stuff, just ask and I'll see what I can do.
onclick="delete_purchase('138')"
calls a Javascript function called delete_purchase, which doesn't seem to exist in your code. You only have a PHP function with that name.
Since all PHP is executed on the server side, the HTML will be built long before the client ever sees the code and therefore you will never be able to call the delete_purchase PHP function from the client side.
The only two ways to get around this are:
- Create a delete_purchase JS function that then calls a PHP file through the use of AJAX.
- Don't call the onclick JS function at all and make the button a regular form submit that you then catch on the server side to delete the purchase. This however would involve a complete page refresh.
Your delete_purchase() function is defined in server-side which is not available in client side. You need to send a request to server and send the id, for example:
?action=delete&id=1
Then you can validate it on server side and call the function
<?php
if(isset($_GET['action']) && $_GET['action'] == 'delete'){
//do some staff
}
?>
you try to call a PHP-function directly from HTML (from browser)
this is impossible!
you may call it using 2 ways:
1) AJAX-call of php-script which will delete the purchase
2) redirect browser to php-script which will delete the purchase and then redirects you back

jquery adding attributes to multiple select drop-downs

I have code (edited, after Brad Christie suggestions):
drupal_add_js('
jQuery(document).ready(function(){
jQuery("#selFinishes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_color . ') {
jQuery(this).attr("selected","selected")
}
})
});
', "inline");
And it sucessfully adds "selected" attribute ($filter_color is added via PHP) to the selected value. But when i target multiple select fields like this:
drupal_add_js('
jQuery(document).ready(function(){
jQuery("#selFinishes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_color . ') {
jQuery(this).attr("selected","selected")
}
})
jQuery("#selThemes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_theme . ') {
jQuery(this).attr("selected","selected")
}
})
});
', "inline");
Both of loops fail to work!
Thanks for tips!
The code above is apparently mixed javascript and php, but it not possible to tell what might be wrong with it. You should check (by viewing source in the browser) whether the resulting javascript code is what you intended. If possible post the resulting javascript here.
My educated guess is that the resulting JavaScript is invalid when it's output. Guessing by the variable names ($filter_color & $filter_theme) one (or both) of them is/are most likely a string making the test if (...val() == string) fail which in terns make the entire block of JavaScript result in a syntax error and fail entirely.
Try changing your output to encase the value in a string (maybe something like the following):
[snip].val() == "' . $filter_theme . '") {[/snip]
Notice I added double quotes on either side of the variable before and after the PHP concatenation. This should fix the syntax would should intern make it work again.
Again, too little information in your question but this would be my guess at the solution

Categories