Php form not working, Works as 2 separate pages but not as 1 combined

- 1 answer

Ad

OK hi, basically I have a PHP form, so you enter something in the form and then on the next page it will display some data depending upon what you put in. I am trying to put this into one webpage rather than 2. It works fine as 2 seperate webpage but when i tried to combine them it doesn't work.

Since when the person first loads the webpage no form would have been submitted meaning that it cant load any data I've done

if (player1 != "")

so that if the php form has been submitted and then that would be true. Of course that's built upon the assumption that with it not submitted it will equal "". I'm not sure if that's right so that's something to pay attention to.

Here is the form

<form action="hacker.php" method="post">
<table>
<tr>
<td>Hackers name:</td>
<td>
<input type="text" name="player" value="" maxlength="100" />
</td>
</tr>
<tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>

And here is the page the form sends information to (hacker.php):

<script>
  var yqlcallback = function(data) {
var results = data.query.results;
document.body.innerHTML = results.span;
var percentage = results.span;
rating = percentage.slice(0, -1);
if (rating > 30) {
    document.body.innerHTML = "This player has now been banned";
} else {
    document.body.innerHTML = "Please submit further evidence to have this person banned";
}
};

</script>

<script id="myscript"></script>

<?php
$player = $_POST['player'];
?>
 <script>
  var player1 = "<?php echo $player; ?>";
  var link1 = "https://query.yahooapis.com/v1/public/yql?q=select%20content%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.team-des-fra.fr%2FCoM%2Fbf3.php%3Fp%3D";
  var link2 = "%22%20and%20xpath%3D'%2F%2F*%5B%40id%3D%22content%22%5D%2Fdiv%5B3%5D%2Fdiv%2Fsp    an'&format=json&callback=yqlcallback";
  var link = link1 + player1 + link2;
   document.getElementById('myscript').setAttribute('src', link);
</script>

I tried to combine those 2 into one php file called index.php and make it so that the javascript will only load data if it gets information from a from but it doesn't seem to be working. Hers what i have: The form action is index.php and the name of the page is index.php, I've done this because I don't want it to redirect to a separate link once the form is submitted.

<!--This is the form used to submit the info-->
<center>
<form action="index.php" method="post">
<table>
<tr>
<td>Hackers name:</td>
<td>
<input type="text" name="player" value="" maxlength="100" />    
</td>
</tr>
<tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>

<!--This gets the data from another website and displays text depending upon what the value of that data is-->
<script>
  if (player1 != "") {
  var yqlcallback = function(data) {
    var results = data.query.results;
    document.body.innerHTML = results.span;
    var percentage = results.span;
rating = percentage.slice(0, -1);
    if (rating > 30) {
        document.body.innerHTML = "This player has now been banned";
    } else {
        document.body.innerHTML = "Please submit further evidence to have this person banned";
}
};
};

</script>

<!--This is changed so that the src is dependant upon what the person entered into the form, code that does that is below. Code above then reads the data sheet this gets-->
<script id="myscript"></script>


<!--This gets the value from the php form then converts it to javascript and creates a link with it used to get the data on that person-->
<?php
$player = $_POST['player'];
?>
<script>
  var player1 = "<?php echo $player; ?>";
  var link1 = "https://query.yahooapis.com/v1/public/yql?q=select%20content%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.team-des-fra.fr%2FCoM%2Fbf3.php%3Fp%3D";
  var link2 = "%22%20and%20xpath%3D'%2F%2F*%5B%40id%3D%22content%22%5D%2Fdiv%5B3%5D%2Fdiv%2Fspan'&format=json&callback=yqlcallback";
  var link = link1 + player1 + link2;
  if (player1 != "")  {
   document.getElementById('myscript').setAttribute('src', link);
  }
</script>
Ad

Answer

Ad

You are defining the player1 variable after it is being checked in the script above. Try moving

<script>
  if (player1 != "") {
  var yqlcallback = function(data) {
    var results = data.query.results;
    document.body.innerHTML = results.span;
    var percentage = results.span;
rating = percentage.slice(0, -1);
    if (rating > 30) {
        document.body.innerHTML = "This player has now been banned";
    } else {
        document.body.innerHTML = "Please submit further evidence to have this person banned";
}
};
};

</script>

To below the other script.

Ad
source: stackoverflow.com
Ad