0

Ok so i am trying to build a battle script in php / ajax. At the moment i have a html form with the users moves and a text box ( to test ) i am using ajax to send the move to a parse file which then echo's out which move they have picked. The problem i have is that i know how to make it so when the user submit the form the variable which stores there health go down but i do not know how to show it on the first page. So i have page1 the health is showed and the user picks a attack and then submits the ajax will send the move over to page 2 and makes the health go down but i do not know how to make the health go down on page 1 with out refreshing ?Even tho i make it go down when the user submits i do not know how to make it show dynamically. I hope i have explained the best i can.

here is what i have coded so far.

My ajax

<script language="JavaScript" type="text/javascript">
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "my_parse_file.php";
    var sF = document.getElementById("selectionField").value;
    var ln = document.getElementById("last_name").value;
    var vars = "selectionField="+sF+"&lastname="+ln;
    hr.open("POST", url, true);


    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
   if(hr.readyState == 4 && hr.status == 200) {
   var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
   }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}



</script>
 <script type="text/javascript">
var x = <?php echo $phpx; ?>;
 var div = document.createElement("DIV");
 div.appendChild(document.createTextNode(x));
 document.body.appendChild(div);
 </script>

Then the div and html form

 <select size="3" name="selectionField" id="selectionField" multiple="no" > 
    <option value="move1" >California -- CA </option>
    <option value="move2" >Colorado -- CO</option>
    <option value="move3" >Connecticut -- CN</option>
  </select>
  <br />
  <br />
  Your Last Name: 
  <input id="last_name" name="last_name" type="text" />
  <br />
  <br />
  <input name="myBtn" type="submit" value="Submit Data" onClick="javascript:ajax_post();">
  <br />
  <br />
</p>

</p>

<div id="status"></div>

so if i echo out the health like normal in php on page 1 the user picks a atatck and then it gets sent to page 2 dynamically page 2 makes there hp go down / php session go down. but because i would be echoing out the variable in php on page one it would not change dynamically . So i am guessing i would have to store the variable in side ajax ?

edit here is a example of what you guys have subjected.

       <script type="text/javascript">
    $.ajax({
      url: 'my_parse_file.php',
      type: 'POST',
      data: '{id: $('#nick').val() }'
      success: function(data){
       $('#hp').html(data);
      }
      }
    });
     </script>

<div id="hp"><?php echo $nick ; ?></div>

this is the code i have added tot he first page then in side my_parse_file.php i have $nick = '1'; but it is not displaying anything...

NikiC
  • 100,734
  • 37
  • 191
  • 225
user2442903
  • 23
  • 1
  • 5

2 Answers2

1

You could do something similar to this: Jquery checking success of ajax post

Then you don't really need the submit, but just update the current page by pure jquery.

Edit

<script language="JavaScript" type="text/javascript">
    $(function() {
        $('#buttonAttack').click(function (){
        $.ajax({
                url: 'my_parse_file.php',
                type: 'POST',
                data: {id: $('#nick').val() }
                success: function(data){
                    $('#hp').html(data);
                }
             });
    });
    });
</script>
<div id="hp"></div>
<input id='buttonAttack' type='button' value='Attack!' />
Community
  • 1
  • 1
aws
  • 26
  • 3
  • could i ask what you mean by "just update the current page by pure jquery " i am guessing you mean storing the variable in side jquery and then just printing them out on the page ?? – user2442903 Jul 03 '13 at 13:23
  • You would make the post to the server and in the success function, update your e.g. input, div, label, select element. Like in azngunit81' answer. – aws Jul 03 '13 at 13:32
  • i have edited my post to include the coding which you guys have subjected just wanted to make sure im doing it right ? – user2442903 Jul 03 '13 at 13:42
  • Well you to use the script you would put it in a function, and then do a click event on your button that calls said function. Sorry my browser is acting up so I can't do code example just yet :) – aws Jul 03 '13 at 13:54
  • That is what im tyring to say i have the main script up above which when i click the submit button will edit the hp variable but i do not know how to edit it on my first page. I don't want it so the user has to click the submit button to view there hp i need ti displayed on the page... – user2442903 Jul 03 '13 at 13:58
  • a click the button but it does nothing... it says there is a error with the line data: '{id: $('#nick').val() }' – user2442903 Jul 03 '13 at 21:26
  • yeah should be data: { id: $('#nick').val() } without the ' before and after { }. This is of course assuming that you also have an input field that has a value. Otherwise you will not get any data. If you have it stored in a e.g. div, p, or label then you need to get the html. – aws Jul 03 '13 at 21:59
  • i store the $nick on the my_parse_file.php which increases it and decreases it but of course using normal php will not echo it out live.. that is why i am hoping this does – user2442903 Jul 03 '13 at 22:33
0

Step 1) switch your script to jquery ajax (recommended for sanity)

which in turn will result in

$.ajax({
  url: 'your_php_page.php',
  type: 'POST',
  data: '{id: $('#last_name').val() }'
  success: function(data){
   $('#status').html(data);
  }
  }
});

URL: is the link to your php data: is all variable $_POST that the php will see

where your_php_page.php does your fetch/model and echo out the result. On finish the "data" is the echo returned which you will replace #status id with whatever html you want.

EDIT: To answer your added code - you are mixing your php and javascript wrong

with $('#hp').html(data) you are already echo-ing back the data received from the php file. thats why javascript is client side and php is server side.

azngunit81
  • 1,574
  • 2
  • 20
  • 38
  • There is a error with your code on line type: 'POST', it says. – user2442903 Jul 03 '13 at 13:22
  • could you look at my edited code to see a example of what i think you mean ?? I just need a div to be echo out a variable i set on the your_php_page.php when i click the submit button... – user2442903 Jul 03 '13 at 14:19
  • take out and you should be fine, as i mentioned the html(data) takes care of replacing that div with data...now if thats not working then there might be other spot in your code not working ...dont forget to link the jquery library. – azngunit81 Jul 03 '13 at 15:05