-2

I've been dealing for half the day with this. My function is not working. It worked at one point and now I can't figure out how to go back. This is my HTML code

<a href="#" onclick="updateDB('<?php echo $a; ?>','<?php echo $b; ?>','<?php echo $c; ?>','<?php echo   
$d; ?>','<?php echo $e; ?>','<?php echo $f; ?>','<?php echo $g; ?>','<?php echo $h; ?
>','<?php echo $i; ?>','<?php echo $j; ?>','<?php echo $k; ?>')">Test</a>

Here is my javascript code

<script>    
$(function() {

   function updateDB(a,b,c,d,e,f,g,h,i,j,k)
   {
        $.post("update.php",
        { a:a,b:b,c:c,d:d,e:e,f:f,g:g,h:h,i:i,j:j,k:k 
        });

    alert("Finally");
   }
 }
</script>

I can't get it to say Finally.

Any help is greatly appreciated.

Thanks,

8
  • What is your html output?
    – bwoebi
    Commented Jul 21, 2013 at 19:00
  • 6
    Please help yourself and give your variables sane names.
    – PeeHaa
    Commented Jul 21, 2013 at 19:00
  • Also you may want to look into arrays and implode()
    – PeeHaa
    Commented Jul 21, 2013 at 19:01
  • 2
    The first problem is that its unfreakingreadable. Have you ever even heard of arrays?
    – cHao
    Commented Jul 21, 2013 at 19:18
  • 1
    onclick="updatedb(<?= implode(',', array_map('json_encode', array($a, $b, $c, $d, $e...))) ?>" would be safer, as well. If any of those values is null, just echoing them into the page will make the JS syntax invalid. And that's not even going into what happens if they contain strings, quotes and HTML.
    – cHao
    Commented Jul 21, 2013 at 19:26

1 Answer 1

1

Because you are using an <a> tag, you need to stop the default execution of such a link. The easiest would be:

onclick="updateDB(...); return false;"

That being said, since you already use jQuery, do yourself a favor and use jQuery's event handlers, e.g:

<a href="#" id="testlink">Test</a>

<script>
    $('#testlink').click(function(event){
        event.preventDefault();
        updateDB('<?php echo $a; ?>','<?php echo $b; ?>','<?php echo $c; ?>','<?php echo   $d; ?>','<?php echo $e; ?>','<?php echo $f; ?>','<?php echo $g; ?>','<?php echo $h; ?>','<?php echo $i; ?>','<?php echo $j; ?>','<?php echo $k; ?>');
    });
</script>

Even better would be to attach the variables you get from PHP and add them to a data- attribute, so you can reuse the click handler:

<a href="#" class="testlink" data-my-value="1">Test 1</a>
<a href="#" class="testlink" data-my-value="2">Test 2</a>

<script>
    $('.testlink').click(function(event){
        event.preventDefault();
        var data = $(this).attr('data-my-value');
        updateDB(data);
    });
</script>

UPDATE: To still redirect to another page, I would do this (untested):

<a href="newpage.html" class="testlink" data-my-value="1">Test 1</a>

<script>
    $('.testlink').click(function(event){
        event.preventDefault();
        var data = $(this).attr('data-my-value'),
            url = $(this).attr('href');
        updateDB(data, url);
    });

    function updateDB(data, url)
    {
        $.post("update.php",
            data, 
            function() {
                window.location = url;
            }
        );
    }
</script>
4
  • Thanks for that Steve. Works great however, I should have changed my initial code. I do want it to go to another page though. I want it to run the code and them go to another page. Any suggestions? Commented Jul 21, 2013 at 19:13
  • And also, I would prefer to use onclick because I'm getting the variables as I go down the page and then update the database once I click the link. Commented Jul 21, 2013 at 19:14
  • I just updated my code to give an example how you can use data- attributes to do what you want and still have reusable code. I would highly advice against onclicks, especially if you are already using a library.
    – Steve
    Commented Jul 21, 2013 at 19:16
  • If you want to go to another page, you should have a link in the href. I will update my code to show an example.
    – Steve
    Commented Jul 21, 2013 at 19:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.