Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PHP to echo out Jquery to perform .post(); on .click(); results in a query that I cannot explain
    text
    copied!<p>I've searched, I've edited, re-edited, I've been condescended by JSLint and have re-tested. I've reached the practical limit of what I know, and I don't have the experience with Firebug or other tools to isolate the issue any more specifically. Point a young boy in the right direction.</p> <p>Here is what I think is the offending code:</p> <pre><code>echo '&lt;script type="text/javascript"&gt;' . "\n"; echo '$(document).ready(function() {' . "\n"; echo '$("#' . $row['firstname'] . $row['lastname'] . '").click(function() {' . "\n"; echo ' $.post("http://example.com/publish.php", { fname: "' . $row['firstname'] . '", lname: "' . $row['lastname'] . '", input: "' . $published['input'] . '", table: "' . $q . '" });' . "\n"; echo 'e.preventDefault();' . "\n"; echo 'return false;' . "\n"; echo ' });' . "\n"; echo '});' . "\n"; echo '&lt;/script&gt;'; </code></pre> <p>Which is part of a larger while loop that fetches data from a mysql DB detailing individuals who have been assigned an html template as part of an outreach scheme. Amongst other things, the loop lets a user know if the content has been published to the web, or if it's just sitting as part of a query, ready to go; 0 for unpublished, 1 for published. The script above is expected to tell publish.php to perform an UPDATE query and change 0 to 1 or vice versa. Seems simple enough, and if I manually query the script, my content will publish— I'll get the expected result.</p> <p>Now on the browser side I've used PHP to output DB information inside a table. In the last column, there is either a button to publish, or unpublish.</p> <pre><code>echo "&lt;td&gt;&lt;button id='" . $row['firstname'] . $row['lastname'] . "' class='btn btn-small " . $published['UI'] . "'&gt;" . $published['do'] . "&lt;/button&gt;&lt;/td&gt;"; </code></pre> <p>The above code seems to work fine; it just generates a unique #id based off an amalgamation of their first and last names, echos the appropriate class to attach to the button (red for unpublish, green for publish) and alerts the user of the action they will take should they choose the press the button.</p> <p>Now, it's supposed to all come together with the following. Which is the output of what I think may be the offending code (the first snippet I posted). To me it looks sweet, but who knows. I am an amateur after all.</p> <pre><code>$(document).ready(function() { $("#FirstnameLastname").click(function() { $.post("http://example.com/publish.php", { fname: "Firstname", lname: "Lastname", input: "1", table: "Tablename" }); e.preventDefault(); return false; }); }); </code></pre> <p>If I write:</p> <pre><code>$.post("http://example.com/publish.php", { fname: "Firstname", lname: "Lastname", input: "1", table: "Tablename" }); </code></pre> <p>Directly into a console, I'll change the publish status from 0 to 1 for user Firstname Lastname in the Tablename. If I click the button, I'll get a nonsensical string in the address bar:</p> <pre><code>http://example.com/outreach.php?names=Tablename&amp;names=None </code></pre> <p><strong>What is even up with this?</strong></p> <p>Thanks in advance for even reading this far. Looking forward to figuring this out! haha.</p> <p>Additional notes:</p> <pre><code>PHP 5.3.x Jquery is called in the head. All table names contain a number of dashes - could this be causing the string to turn into rubbish? e.g outreach.php?names=Ta-bl-ena-me&amp;names=None </code></pre> <p>Below is how outreach.php requests a list of Tablenames. On change, this is sent as Ajax to getnames.php which then sends back html/js to outreach.php (no problemo) in the form of the table mentioned previously. This is why I'm now thinking I may need to go back to the drawing board...</p> <pre><code>&lt;?php while($showtablerow = mysql_fetch_array($showtablequery_result)) { echo "&lt;option&gt;" . $showtablerow[0]."&lt;br /&gt;" . "&lt;/option&gt;"; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload