Web Analytics Made Easy -
StatCounter Ajax help - Ssend 2 variables using a href and allow some to repeat - CodingForum

Announcement

Collapse
No announcement yet.

Ajax help - Ssend 2 variables using a href and allow some to repeat

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved Ajax help - Ssend 2 variables using a href and allow some to repeat

    I have the below code I am testing, but would like to do 2 things

    1. I would like to send 2 variables from the link <a href the sendto and sentby is that possible?
    I don't want to use a form, but I did think about javascript submit but didn't know how to do it.

    2. How would I go at allowing the user to repeat the process, after the message has faded out, and get the same message back?



    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
    $("#test").click(function() {
    var sendto = 1;
    var sentby = 99;
    var info = 'sendto=' + sendto + '&sentby=' + sentby;

    $.ajax({
    type: "POST",
    url: "testajax.php",
    data: info,
    cache: false,
    success: function(html) {
    $("div#update").append(html);
    }
    });
    $(".flash").fadeOut(10000);
    return false;
    });
    })
    </script>

    <div id="update" class="flash"> </div>

    <a href="#" id="test">test click me</a>


    testajax.php code

    <?php
    if($_POST) {

    $got = 'got';
    $sendto = $_POST['sendto'];
    $sentby = $_POST['sentby'];
    // do something

    } else {
    }

    ?>
    <div class="flash"><? echo $got; ?> test message output Send to:<? echo $sendto; ?> - Sent by <? echo $sentby; ?></div>


    I need to change the .php file a bit.

    But any pointer on how to improve the code would be nice. I am new to Ajax and have got most of the code from 9lessons.info
    Last edited by CBG; Sep 11, 2011, 07:55 PM.
    Regards,
    CBG

  • #2
    you could use data- attribs to store the passed values in your html.
    but it looks like you want user input, which means forms.

    jQuery can serialize a whole form with one call (http://api.jquery.com/serialize/), and post it the same as a form more-or-less how you have it posted.

    you could also send objects from js to php using JSON.stringify() on the client and json_decode() on the php side. this lets you have deep objects, strong numbers+booleans, and n+ repeatable values such as history logs.

    i'm not sure what you're asking about in question 2...
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

    Comment


    • #3
      Thanks for the reply.
      I can't use a form with a submit button in the case of this.

      It has to be submitted via a link.
      I have tried:

      <form name="send" method="post">
      <input type="hidden" id="sendto" name="sendto" value="1">
      <input type="hidden" id="sentby" name="sentby" value="99">
      </form>

      <a href="#" onclick="document.send.submit();" id="test">test click me</a>


      But the problem with this the message which is sent via the PHP script disappears straight away. It seems to reload the page and not do ajax
      Last edited by CBG; Sep 11, 2011, 07:27 PM.
      Regards,
      CBG

      Comment


      • #4
        Hi,

        I got the below working how I want it
        Just thought I post for reference.


        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(function() {
        $("#test").click(function() {
        $("#send").ready(function(){
        var sendto = $("#sendto").val();
        var sentby = $("#sentby").val();
        var info = 'sendto=' + sendto + '&sentby=' + sentby;

        $.ajax({
        type: "POST",
        url: "testajax.php",
        data: info,
        cache: false,
        success: function(html) {
        $("div#update").prepend(html);
        $("div#update").fadeIn("fast");
        $(".flash").fadeOut(10000);
        }
        });
        });
        return false;
        });
        });
        </script>

        <form id="send" name="send" method="post">
        <input type="hidden" id="sendto" name="sendto" value="1">
        <input type="hidden" id="sentby" name="sentby" value="99">
        </form>

        <div id="update" class="flash"> </div>
        <a href="#" id="test">test click me</a>


        All problems are fixed
        It fixed problem 2 as well, where it would not display the message on a second or more clicks, so would display in once and that it, but adding fadein fixed it I think.
        Last edited by CBG; Sep 11, 2011, 07:58 PM.
        Regards,
        CBG

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎