I have a list of news from a database.  When I start the page all the news titles appear in a list and when I click on them the content of that specific news will show up in a div below.  I'm getting the initial news list like this (this is php code from the index.php file that runs on startup)  $sql = "SELECT title FROM `news`"; $result = mysqli_query($GLOBALS['conn'], $sql);   if (mysqli_num_rows($result) &gt; 0) {         echo "&lt;div id = 'list'&gt;";         echo "&lt;p id = 'text'&gt; All News:  &lt;/p&gt;";         echo "&lt;ul id ='news_list'&gt;";         while($row = mysqli_fetch_assoc($result)) {             echo "&lt;li&gt;&lt;a href='#'&gt;" . $row["title"] . "&lt;/a&gt;&lt;/li&gt;";         }         echo "&lt;/ul&gt; &lt;/div&gt;"; }   }  And in my script.js I have a function that will show a specific news content when I click on one  $( "#news_list li" ).click(function() {         //do something  });    This works, however I want to sort those news based off a category and view only those specific news instead (instead of all of them).   So my script.js function would look something like  $( ".category" ).click(function() {         var hash = $(this).text(); //name of the news         $("#news_list").empty();   // ul of news list         $.post("get_news_by_category.php",{ajax: true, hash:hash},function(data, status){                         var news = "";                         for (var i = 0; i &lt; data.length; i++) {                             if (data.charAt(i) != "$"){                                 news = news + data.charAt(i);                             }                             else{                                 $( "#news_list" ).append("&lt;li&gt;&lt;a href='#'&gt;" + news +  "&lt;/a&gt;&lt;/li&gt;");                                 news = "";                             }                         }         });           $("#text").text('News by ' + hash);   And my get_news_by_category.php looks something like this  $sql = "SELECT title FROM news WHERE category = '" . $_POST['hash'] . "'";         $result = mysqli_query($GLOBALS['conn'], $sql);         if (mysqli_num_rows($result) &gt; 0) {             while($row = mysqli_fetch_assoc($result)) {                 echo $row["title"]; echo "$";             }         }      I get the new list with the desired news, but I no longer can click on them. I can only click the news from the initial list, however if I try to click on a news from the list after I filter it, that won't work and I have no idea why.  Hope I've made myself clear.  I would love some input on what I'm doing wrong  EDIT:   I can't thank CBroe and Just a Student enough for the fast response. The solution was using   $('#list').on('click', 'li', function() instead of my old click function and that actually worked.
112
I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off.  This happens on page ready and works just fine.  The problem I have is that any select boxes I add via Ajax or DOM after the initial loop won't have the event bound.  I have found this plugin (), but before I add another 5k to my pages with a plugin, I want to see if anyone knows a way to do this, either with jQuery directly or by another option.
94609
I'm working at an app which would make a POST ajax request to a PHP script on my server. The script would query the database and return a row of records, as an array. (One array for each row, containing elements such as id, title, etc). I then want to use json_encode() to encode this array, and pass it back to the javascript which will use it to display the records.  1) How can I return the JSON encoded string to the javascript? 2) How will the javascript loop through the rows and access their fields?
104708
I'm working on a web page where I'm making an AJAX call that returns a chunk of HTML like:   &lt;div&gt;   &lt;!-- some html --&gt;   &lt;script type="text/javascript"&gt;     /** some javascript */   &lt;/script&gt; &lt;/div&gt;   I'm inserting the whole thing into the DOM, but the JavaScript isn't being run.  Is there a way to run it?    Some details: I can't control what's in the script block (so I can't change it to a function that could be called), I just need the whole block to be executed.  I can't call eval on the response because the JavaScript is within a larger block of HTML.  I could do some kind of regex to separate out the JavaScript and then call eval on it, but that's pretty yucky.  Anyone know a better way?
54022
I'm attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I'd like the script to simply send a response indicating that the process has started, but JQuery won't return the response until the PHP script is done running.  I've tried this with a "close" header (below), and also with output buffering; neither seems to work. Any guesses? or is this something I need to do in JQuery?  &lt;?php  echo( "We'll email you as soon as this is done." );  header( "Connection: Close" );  // do some stuff that will take a while  mail( 'dude@thatplace.com', "okay I'm done", 'Yup, all done.' );  ?&gt;
66844
How do you combine two jQuery search results? eg:  var $allFoos = $('.foo'),     $allBars = $('.bar')     $allFoosAndBars = $allFoos + $allBars;   Obviously, I just made up that last line, but I hope it makes it sorta clear what I mean. To be clear, the example is greatly simplified, and it could be any arbitrary sets i'm talking about, so $('.foo, .bar') is not what I'm after.
64820
If you know the Index, Value or Text. also if you don't have an ID for a direct reference.  ,  and  are all helpful answers.  Example markup  &lt;div class="selDiv"&gt;   &lt;select class="opts"&gt;     &lt;option selected value="DEFAULT"&gt;Default&lt;/option&gt;     &lt;option value="SEL1"&gt;Selection 1&lt;/option&gt;     &lt;option value="SEL2"&gt;Selection 2&lt;/option&gt;   &lt;/select&gt; &lt;/div&gt;
103134
I have come to the conclusion that my PHP installation and my MySQL installation are not getting along; I’m working on making my first web page that uses MySQL for content, but after two days, man pages, online documentation, and dozens of tutorials, I just can’t get the PHP/MySQL part to work.  I have an established home web server (I am the only admin/user) that runs Apache and hosts my web pages.  I have MySQL installed, and was able to create and query a database through Terminal.  I have PHP installed, and can use echo to print text to the document.  When I work with MySQL however, everything changes…  I have a page that contains initialization stuff in .  contains a few paragraphs of hardcoded HTML and then PHP code to access MySQL.  When I first loaded the page, all that resulted was a blank page.  I commented out the PHP section and reloaded to confirm that the plain HTML still worked, which it did.  Therefore, some part of PHP was keeping any of the page from ever loading.    To begin troubleshooting my code, I left all of the PHP code commented except the following code for the server connection, which is adapted from several different tutorials:  &lt;? echo("&lt;p&gt;PHP is working&lt;/p&gt;");  $ses = mysql_connect("localhost","username","password"); if(!$ses){  echo("&lt;p&gt;Connection to content server failed.&lt;/p&gt;");  exit(); } echo("&lt;p&gt;Database Connected&lt;/p&gt;"); ?&gt;   When the page is loaded, the beginning HTML loads in addition to a paragraph reading “PHP is working” but neither the failure or success message appear.  I removed the If structure from the code - so that only two echos and the mysql_connect remained – and got the same result.    It seems that the mysql_connect feature is causing the server to stop loading the code when it is run, but I’m out of ideas for how to fix it.  For the record: I’ve modified the php.ini file to include the “connection = msql.so” line, and I’m using MySQL 5.5, Apache 2.2.22, PHP 5.3.10, Ubuntu 12.04.  Also, both my router and modem have been configured to forward port 3306 to the server – though I don’t see how that should be necessary – and my MySQL username and password have been triple checked and are identical to the ones that I use successfully in Terminal.  What am I missing?
56524
I needed a way to filter a page/posts content before it was loaded so I could add scripts to the header if a specific shortcode was present. After much searching I came across this on    function has_my_shortcode($posts) {     if ( empty($posts) )         return $posts;      $found = false;      foreach ($posts as $post) {         if ( stripos($post-&gt;post_content, '[my_shortcode') )             $found = true;             break;         }      if ($found){         $urljs = get_bloginfo( 'template_directory' ).IMP_JS;      wp_register_script('my_script', $urljs.'myscript.js' );      wp_print_scripts('my_script'); }     return $posts; } add_action('the_posts', 'has_my_shortcode');   which is absolutely brilliant and did exactly what I needed.   Now I need to extend it a bit further and do the same for sidebars. It can be by a particular widget type, shortcode, code snippet or anything else that would work to identify when the script needs to be loaded.  The problem is I can't figure out how to access the sidebars content before the sidebar is loaded (the theme in question will have several sidebars)
80948
I have an HTML page with a typical structure:  &lt;html&gt;   &lt;head&gt;    &lt;script src="..." &gt;&lt;/script&gt;    &lt;style&gt;...&lt;/style&gt;   &lt;/head&gt;   &lt;body&gt;    content   &lt;/body&gt;   &lt;script&gt;     var success_callback = function(data) {       // REPLACE PAGE CONTENT &amp; STRUCTURE WITH "data"     }     ajax(url, params, success_callback);   &lt;/script&gt; &lt;/html&gt;   Do you think it is possible ?  I've already tried to give the html tag an id and doing $(id).replace(data); with no success.  Don't ask me why, but that is what I need (I'm working with a special "mashup builder" site... it is a long story).  EDIT : I forgot to say that scripts in the received content have to be executed, even external scripts included using &lt;script src="..."&gt;.
16554
When I create a form using the form API, the resulting html includes all sorts of unwanted divs. For example, if I use:  $form = array(     'myFieldset' =&gt; array(         '#type' =&gt; 'fieldset'     ) );   The resulting html looks like this:   &lt;form action="/drupal-7.15/?q=chercheur" method="post" id="form-chercheur" accept-charset="UTF-8"&gt;     &lt;div&gt;         &lt;fieldset class="form-wrapper" id="edit-myfieldset"&gt;             &lt;div class="fieldset-wrapper"&gt;             &lt;/div&gt;         &lt;/fieldset&gt;         &lt;input type="hidden" name="form_build_id" value="form-87Dv5ezVpaLI5mD345ofMkLgPhsv_dawudUp73ePQTc" /&gt;         &lt;input type="hidden" name="form_token" value="sXXINWr7rmParm0tRJoEb6QKGIH_QH-ffRSOtnvxYY0" /&gt;         &lt;input type="hidden" name="form_id" value="form_Chercheur" /&gt;     &lt;/div&gt; &lt;/form&gt;   As you can see, two extraneous divs are added here. They are making the javascript I'm writing uselessly complicated, so I was wondering if there was a way to remove the extraneous divs. Ideally, I would like to obtain something like this:  &lt;form action="/drupal-7.15/?q=chercheur" method="post" id="form-chercheur" accept-charset="UTF-8"&gt;     &lt;fieldset class="form-wrapper" id="edit-myfieldset"&gt;     &lt;/fieldset&gt;     &lt;input type="hidden" name="form_build_id" value="form-87Dv5ezVpaLI5mD345ofMkLgPhsv_dawudUp73ePQTc" /&gt;     &lt;input type="hidden" name="form_token" value="sXXINWr7rmParm0tRJoEb6QKGIH_QH-ffRSOtnvxYY0" /&gt;     &lt;input type="hidden" name="form_id" value="form_Chercheur" /&gt; &lt;/form&gt;   Is this possible?
40753
Recently I have started playing with jQuery, and have been following a couple of tutorials. Now I feel slightly competent with using it (it's pretty easy), and I thought it would be cool if I were able to make a 'console' on my webpage (as in, you press the ` key like you do in  games, etc.), and then have it Ajax itself back to the server in-order to do stuff.  I originally thought the best way would be to just get the text inside the textarea, and then split it, or should I use the keyup event, convert the keycode returned to an ASCII character, append the character to a string and send the string to the server (then empty the string).  I couldn't find any information on getting text from a textarea, all I got was keyup information. Also, how can I convert the keycode returned to an ASCII character?
13487
Using Drupal 7.14 with the PHP filter on I have the following in a Basic Page content type:  &lt;?php if ($is_mobile == TRUE):?&gt; &lt;div class="fb-like-box" data-href="http://www.facebook.com/a page" data-width="710" data-height="600" data-show-faces="false" data-border-color="#73AA4B" data-stream="true" data-header="false"&gt;&lt;/div&gt; &lt;?php else: ?&gt; &lt;div class="fb-like-box" data-href="http://www.facebook.com/a page" data-width="900" data-height="600" data-show-faces="false" data-border-color="#73AA4B" data-stream="true" data-header="false"&gt;&lt;/div&gt; &lt;?php endif; ?&gt;   I get the error message:  Notice: Undefined variable: is_mobile in eval() (line 1 of D:\websites\modules\php\php.module(80) : eval()'d code).  I sort of understand - I'm trying to grab a variable from the Drupal system but is there any way to do this? Maybe using Tokens?
101903
For my website, I am using a static home page page Home (template front-page.php) and for posts- page News (template home.php)  I need to show all the post titles and exceprts on that page News (templ. home.php), but also show the original title and the_content from that page News itself.  This code below while statement shows all the posts, which is good, but the top the_title also shows the title from the first post, not from the News page itself.  How to achieve what I need?  &lt;?php get_header(); ?&gt;      &lt;?php     the_title();           the_content();     ?&gt;  &lt;?php if(have_posts()) while(have_posts()) : the_post(); ?&gt;          &lt;?php         the_title();         ?&gt;          &lt;?php         the_content();         ?&gt;      &lt;?php     endwhile;     ?&gt;    &lt;?php get_footer(); ?&gt;
115687
I've got a list of links which have a click event attached to them, I need to get the ID from the child A link. So in the example below if I clicked the first list element I'd need google retuned.   I've tried '$this a' but can't quite work out the syntax.  $("ul li").click(function(event){   $("input").val($(this).html());              });     &lt;ul&gt;     &lt;li&gt;&lt;a href="http://www.google.com" id="google"&gt;Google&lt;/a&gt; &lt;/ul&gt;
83765
This AJAX function gets called on a click:  $.ajax({             url: 'site/ajax.php',             data: {id: $('section#single article input:last-of-type').attr('value'), cote: 'like'}         }).done(function(html) {}   It passes the ID of the post (which is contained in a hidden input) for function get_post_meta() that is located in ajax.php. This is what AJAX.php looks like:  $cote = get_post_meta($_GET['id'], 'cote', true); if($_GET['cote'] == 'like') {     $newCote = $cote++;     update_post_meta($_GET['id'], 'cote', $newCote); } else {     $newCote = $cote--;     update_post_meta($_GET['id'], 'cote', $newCote); }   It is suppose to update the custom post's "cote" field, but the problem is that function get_post_meta() is undefined in ajax.php. This is the error message:  Fatal error: Call to undefined function get_post_meta() in /home2/electro/public_html/beta... on line 2
15556
I am afraid that listing doesn't support javascript.  \begin{lstlisting}[language=javascript] ... \end{lstlisting}   What am I doing wrong?
121705
So I have the following code:   &lt;span class="delete"&gt;&lt;a onclick="return confirm('Are you sure?');" href="&lt;?php echo wp_nonce_url( add_query_arg( array( 'action' =&gt; 'my-delete-product', 'product_id' =&gt; $post-&gt;ID ), my_get_navigation_url('products') ), 'my-delete-product' ); ?&gt;"&gt;&lt;?php _e( '삭제', 'my' ); ?&gt;&lt;/a&gt; | &lt;/span&gt;   It is a simple button.  I want to make this into a shortcode so that I can use it anywhere where appropriate.  Any suggestions?  Thanks.