www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Passing two variables from onclick to xml via javascript

  1. #1
    Join Date
    Sep 2011
    Posts
    5

    resolved [RESOLVED] Passing two variables from onclick to xml via javascript

    Hey everyone,

    I am not quite positive where to post this as this issue may involve more than just one coding language. I am a experienced html/css/php/mysql coder who has been delving into Javascript/XML/AJAX coding.

    Here is what I am attempting to accomplish using a picture of praying hands as a button with an onclick event.

    1. Button is displayed as praying hands that say "Please pray for me".
    2. User/Guest clicks on button.
    3. Username and Prayer Post ID are passed to the prayFor JS function
    4. PrayFor JS function passes Username and Prayer Post ID onto a separate PHP file and the JS changes the original image to one of praying hands that states "Thank You!" on them.

    So far the code works as intended, however when I am passing the prayer ID and then the username and somewhere along the way the username is getting lost as the php files that the JS calls on cannot see that variable. Also the prayer ID is an int and the username is a string.

    Here is what I have so far code wise.

    Javascript/XML bit
    Code:
    <script type="text/javascript">
    var xmlhttp;
    function prayFor(i,u){
    	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    	  xmlhttp=new XMLHttpRequest();
    	}else{// code for IE6, IE5
    	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	xmlhttp.onreadystatechange=function(){
    		if(xmlhttp.readyState==4 && xmlhttp.status==200){
    			document.getElementById("prayFor"+i).innerHTML=xmlhttp.responseText;
    		}
    	}
    	
    	xmlhttp.open("GET", "prayerInc.php?i="+i+"&u="+u);
    	xmlhttp.send();
    }
    </script>
    HTML/PHP bit
    Code:
    <?php $id = 1; $un='Serihon'; ?>
    <input type="image" src="images/prayingHands.png" alt="praying hands" onclick="prayFor('<?php echo $id; ?>','<?php echo $un; ?>');" />
    Any help with this would be greatly appreciated!

    Serihon

  2. #2
    Join Date
    Sep 2011
    Posts
    5
    Here is the php I am calling that uses the u and i variables.
    PHP Code:
    <?php
    include('config.php');

    function 
    clean($str) {
        
    $str = @trim($str);
        if(
    get_magic_quotes_gpc()) {
            
    $str stripslashes($str);
        }
        return 
    mysql_real_escape_string($str);
    }

    $i=clean($_GET['i']);
    $u=clean($_GET['u']);

    $sql "UPDATE pickles SET u='$u' WHERE i='$i' ";
    $result=mysql_query($sql);

    echo 
    "<img src=\"images/prayingHands2.png\" alt=\"Thank you!\" />";
    ?>
    Last edited by serihon; 02-24-2012 at 07:26 AM.

  3. #3
    Join Date
    Sep 2011
    Posts
    5

    Resolved

    Turns out the u was being set and passed into PHP and I had some variables wrong such as a field name in my table. I fixed it and it still wasnt inputting the data into the table correctly.

    Here is the var_dump result.
    array(2) { ["i"]=> string(1) "6" ["u"]=> string(8) "serihon;" }

    If you look closely at the var_dump result you will notice that the username has a semi-colon appended to the end of it. Turns out another issue with this was in the code below.

    Code:
    <input type="image" src="images/prayingHands.png" alt="praying hands" onclick="prayFor('<?php echo $id; ?>','<?php echo "$un;" ?>');" />
    The $un has the semi-colon inside of the parenthesis rather than on the outside which caused this error. I was so determined that it was an error with parenthesis that I somehow botched this portion up. I know my original code doesn't show it but it was an error I fixed.

    So the code is working properly as intended now.

    I would like to thank Dormilich over at CodingForums for the help with this issue.
    http://www.codingforums.com/showthre...31#post1197131

    Frank

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles