PHP text based game input method help

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 17588 Views - Last Post: 06 October 2010 - 10:34 AM

#16 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: PHP text based game input method help

Posted 03 October 2010 - 01:48 PM

Use this perhaps?

Moved to jQuery forum. This is really not a PHP question.
Was This Post Helpful? 1
  • +
  • -

#17 Bugsy6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: PHP text based game input method help

Posted 03 October 2010 - 04:57 PM

Thanks for moving the thread.

So I have made the form print the input into the div but I can't seem to figure out how to use the append() feature to make the input appear after any other text already in the div box?

Here is my message.php file that the input is sent to I think this is where I need to put append(), is that right and if so how do I use append()?

<?php
if(isset($_GET['message'])) 
{
$received = $_GET['message'];
echo "$received";
}
?>




Thanks,
Bugsy
Was This Post Helpful? 0
  • +
  • -

#18 Bugsy6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: PHP text based game input method help

Posted 04 October 2010 - 12:49 PM

So I've tried a few things but still can't get my user input to not replace what was previously inputed!

Any help would be great!

Thanks.
Bugsy
Was This Post Helpful? 0
  • +
  • -

#19 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP text based game input method help

Posted 04 October 2010 - 06:24 PM

Hi,

That is not where you would choose to append rather than replace, what you are showing there is what is being returned from the server.

How are you placing what is returned from the server into your div? Are you using jQuery or straight Javascript? Could we see the code you are using?
Was This Post Helpful? 0
  • +
  • -

#20 Bugsy6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: PHP text based game input method help

Posted 05 October 2010 - 06:24 AM

Ok so here is my code for the user input:

<html>

<head>
<link rel="stylesheet" type="text/css" href="Game_window.css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

function getMessageResponse(str)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObj0ect("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.getElementById('response').innerHTML=xmlHttp.responseText;
      document.myform.message.value = '';
      }
    }
	var url="message.php";
	url=url+"?message="+str; 
	url=url+"&sid="+Math.random();
  	xmlHttp.append("GET",url,true);
  	xmlHttp.send(null);
  }

</script>
</head>
<body>

<form method="POST" name="myform" id="myform">
<input type="text" name="message" size="97" class="Player_Input" />
<input type="button" value="Submit" class="Submit_Input" onclick="getMessageResponse(document.myform.message.value);">
</form>

<div rows="20" cols="40" id="response" name="response" class="Game_Area">


</div>

</body>
</html>



I got it from another site, but on't fully understand it after the document.myform.message.value='' bit, I can't figure out what the code does here.

Here is my message.php form also:
<?php
if(isset($_GET['message'])) 
{
$received = $_GET['message'];
echo "$received";
}
?>




So at the moment this code will accept, process and display the users input, but like most text games I want the input to be a response to text already on the screen. and the problem with this code is it removes anything else in the div box.

Any suggestions on fixing this code or ideas on a better way to do it would be great.

Thanks,
Bugsu
Was This Post Helpful? 0
  • +
  • -

#21 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP text based game input method help

Posted 05 October 2010 - 05:14 PM

You could put each response in a paragraph and then append that to your response div. Something like :

if(xmlHttp.readyState==4)
{
   var para = document.createElement("p");
   para.innerHTML = xmlHttp.responseText;
   document.myform.message.value = '';
   document.getElementById('response').appendChild(para);

}




You most likely want to start working out database requirements next so that your post to the server actually does something. Also this really isn't the right forum for this since your not using jQuery for this.
Was This Post Helpful? 1
  • +
  • -

#22 Bugsy6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: PHP text based game input method help

Posted 06 October 2010 - 08:26 AM

Thanks for that Its working great with that addition.

Now I'm just starting to construct the login part. I'm not sure on how to load in each part of the game, should i have a different file for each room/area or just one long page with all rooms on it?

Thanks,
Bugsy
Was This Post Helpful? 0
  • +
  • -

#23 Bugsy6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 21-September 10

Re: PHP text based game input method help

Posted 06 October 2010 - 10:34 AM

Ok so I've got my code to append to what is in the box already thanks to Jstall.

Now I'm trying to load something if the users input is a specific string, eventually for the login screen.

For example:

Are you new?

Then the user inputs yes or no and it loads the next bit of text depending on the answer.

I have tried this so far can anyone suggest why it just prints the last else "Not a valid input"response part?

<html>

<head>
<link rel="stylesheet" type="text/css" href="Game_window.css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

function getMessageResponse(str)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObj0ect("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
       var para = document.createElement("p");
       para.innerHTML = xmlHttp.responseText;
       document.myform.message.value = '';
       document.getElementById('response').appendChild(para);
    }
    }
	var url="message.php";
	url=url+"?message="+str; 
	url=url+"&sid="+Math.random();
  	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

</script>

</head>
<body>

<form method="POST" name="myform" id="myform">
<input type="text" name="message" size="97" class="Player_Input" />
<input type="button" value="Submit" class="Submit_Input" onclick="getMessageResponse(document.myform.message.value);">
</form>

<div rows="20" cols="40" id="response" name="response" class="Game_Area">
Welcome to the World of Myth are!!<br /><br />

Are you an existing citizen of Myth??<br />

<?php
if ($para=="yes") {
  echo "Welcome back friend!";
}
else if ($para=="no"){
  echo "Well welcome to Myth friend!";
}
else (£para==""){
  echo "Not a valid input!";
}
?>


</div>



</body>
</html>

Thanks,
Bugsy


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2