简体   繁体   中英

Using PHP to echo out Jquery to perform .post(); on .click(); results in a query that I cannot explain

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.

Here is what I think is the offending code:

echo '<script type="text/javascript">' . "\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 '</script>';

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.

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.

echo "<td><button id='" . $row['firstname'] . $row['lastname'] . "' class='btn btn-small " . $published['UI'] . "'>" . $published['do'] . "</button></td>";

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.

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.

$(document).ready(function() {
$("#FirstnameLastname").click(function() {
  $.post("http://example.com/publish.php", { fname: "Firstname", lname: "Lastname", input: "1", table: "Tablename" });
e.preventDefault();
return false;
     });
});

If I write:

$.post("http://example.com/publish.php", { fname: "Firstname", lname: "Lastname", input: "1", table: "Tablename" });

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:

http://example.com/outreach.php?names=Tablename&names=None

What is even up with this?

Thanks in advance for even reading this far. Looking forward to figuring this out! haha.

Additional notes:

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&names=None

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...

<?php   while($showtablerow = mysql_fetch_array($showtablequery_result))    {   echo "<option>" . $showtablerow[0]."<br />" . "</option>";  } ?>

Okay, so far I've just cleaned up your code a bit for readability. I reckon that something goes wrong with posting the parameters. Do they end up in the url as well?

<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $r['firstname'] . $r['lastname']; ?>").click(function(e) {
        $.post("http://thinknextmedia.com/publish.php", 
            { fname: "<?php echo $r['firstname']; ?>", 
              lname: "<?php echo $r['lastname']; ?>", 
              input: "<?php echo $published['input'] ?>", 
              table: "<?php echo $q ?>" });
        return false;
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM