Results 1 to 3 of 3

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

  1. #1
    Join Date
    Sep 2011

    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
    <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");
    		if(xmlhttp.readyState==4 && xmlhttp.status==200){
    	xmlhttp.open("GET", "prayerInc.php?i="+i+"&u="+u);
    HTML/PHP bit
    <?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!


  2. #2
    Join Date
    Sep 2011
    Here is the php I am calling that uses the u and i variables.
    PHP Code:

    clean($str) {
    $str = @trim($str);
    get_magic_quotes_gpc()) {
    $str stripslashes($str);


    $sql "UPDATE pickles SET u='$u' WHERE i='$i' ";

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


    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.

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


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