7 Replies - 607 Views - Last Post: 02 May 2013 - 12:23 PM

#1 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Making a variable go down in javascript

Posted 02 May 2013 - 09:11 AM

So im trying to make a battle script in javascript / jquery . So at the moment i have coded it so the user picks the move and then the page will post the posts to a my_parse_file which will handle it.Here is what i have so far.


Battle.php

<html>
<head>
<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>
</head>
<body>
<h2>&nbsp;</h2>
<p>&nbsp;</p>
<p>

<?php


?>

&nbsp;</p>
<p>&nbsp;</p>
<p>
  <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>
<div id="status"></div>
</body>
</html>






And then my_parse_file.php is simply
<?php 
echo 'Thank you '. $_POST['selectionField'] . ' ' . $_POST['lastname'] . ', says the PHP file';
?>


I need to try and work out how to make it so when i click the submit button it will make a varaible go down and will display the viable on the battle.php with out refreshing the page for there life.

This post has been edited by rpgmaker: 02 May 2013 - 09:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Making a variable go down in javascript

#2 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,848
  • Joined: 30-April 10

Re: Making a variable go down in javascript

Posted 02 May 2013 - 09:41 AM

Have you looked into using the $.post or $.get shortcuts for the ajax connections? I find it faster to write and use as well as easier to read than a block of js ajax.
Was This Post Helpful? 0
  • +
  • -

#3 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Making a variable go down in javascript

Posted 02 May 2013 - 10:34 AM

View Postlaytonsdad, on 02 May 2013 - 04:41 PM, said:

Have you looked into using the $.post or $.get shortcuts for the ajax connections? I find it faster to write and use as well as easier to read than a block of js ajax.



Yes i would know how to post it but i need to display it on the page and then make it go down with out the page refreshing. So what im trying to say is if i echo out $battlehp on the battle.php and then post the $battlehp though the ajax post then still i can not make the $battlehp change on the page if you get me ? I would have to keep on showing messages every time they click the submit button which i do not want.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,389
  • Joined: 12-December 12

Re: Making a variable go down in javascript

Posted 02 May 2013 - 11:46 AM

Don't use a submit button - it is not valid without a form anyway. Submit buttons are intended to submit a form, which is precisely what you don't want to do. Use a button and its click event.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,389
  • Joined: 12-December 12

Re: Making a variable go down in javascript

Posted 02 May 2013 - 11:56 AM

BTW language="Javascript" is obsolete, delete it.

BTWW You mentioned jQuery but you are not using any(?).

onclick="javascript: << this is also obsolete.
Was This Post Helpful? 0
  • +
  • -

#6 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Making a variable go down in javascript

Posted 02 May 2013 - 11:57 AM

I would just like to know how can i display a number on the battle.php which can change with out refreshing the page. So lets say i have a variable on battle.php called $hp on the battle.php page were the submit button is.It will lets say echo out 10 ? When a user submits the button the my_parse_file.php will make it go down by 1 and then the 10 will become 9. I know how to make it echo out variable once the button is clicked but i need to echo it out before its pressed and for it to change with out refreshing. The my_parse_file.php will be making the variable go down i know how to do that but do not know how to show the variable and make it change with out refreshing.I hope i make sense.

This post has been edited by rpgmaker: 02 May 2013 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,389
  • Joined: 12-December 12

Re: Making a variable go down in javascript

Posted 02 May 2013 - 12:11 PM

I suspect there is a fundamental misunderstanding here. Javascript (and HTML) have no knowledge of PHP variables. When a PHP script is used to construct a page it can embed a variable's value in the page, either as a JS variable:

<script type="text/javascript">
var x = <?php echo $phpx; ?>;


or embedded in the HTML somewhere:

<input type="hidden" value="<?php echo phpx;?>" .. >


With Ajax you can, for example, post a value to a php-script. Then, in the php-script, it can refer to this post-data and return an increased value:

<?php
if (isset($_POST['x']) && is_numeric($_POST['x'])) {
    $tempx = intval($_POST['x'], 10);
    echo $tempx++;
    // if you are decrementing, also check that it is > 0.
}
?>

This post has been edited by andrewsw: 02 May 2013 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#8 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Making a variable go down in javascript

Posted 02 May 2013 - 12:23 PM

Ok so this is what ive done on the battle.php i have added

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



and then on the my_parse_file.php i have added

if(!isset($_SESSION['phpx'])){
$_SESSION['phpx'] = '10' ;
}



so when i click the submit button is should show 10 but doesn't ... I have to refresh the page and then it shows 10....

This post has been edited by rpgmaker: 02 May 2013 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1