dongzhangji4824 2012-11-19 03:02
浏览 36
已采纳

使用PHP回显Jquery来执行.post(); on .click(); 导致我无法解释的查询

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">' . "
";
echo '$(document).ready(function() {' . "
";   
echo '$("#' . $row['firstname'] . $row['lastname'] . '").click(function() {' . "
";
echo '  $.post("http://example.com/publish.php", { fname: "' . $row['firstname'] . '", lname: "' . $row['lastname'] . '", input: "' . $published['input'] . '", table: "' . $q . '" });' . "
";
echo 'e.preventDefault();' . "
";
echo 'return false;' . "
";
echo '     });' . "
";
echo '});' . "
";
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>";  } ?>
  • 写回答

1条回答 默认 最新

  • dsafq2131321 2012-11-19 03:16
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)