0

I'm working on a PHP code that checks the status of a server. The code works perfectly when it's used like this:

<?php
$SERVER_IP = "217.198.136.31";
$SERVER_PORT = "25565";
$QUERY_PORT = "25565";
?>

However, instead of manually assigning the values for the php variables I need to get them from two HTML input elements. I've tried the following code but it doesn't work:

<input id="checkip" name="checkip" value="217.198.136.31">
<input id="checkport" name="checkport" value="25565">

<?php
$SERVER_IP = $_POST['checkip'];
$SERVER_PORT = $_POST['checkport'];
$QUERY_PORT = $_POST['checkport'];
?>

I can't figure out why it isn't working, doesn't the $_POST get the exact value from the HTML elements and put them in the PHP variables? What should I do to get the same result for the last code?

2
  • 2
    Are you sending a POST request? Commented Nov 10, 2014 at 7:23
  • wrap inside a form and submit Commented Nov 10, 2014 at 7:26

5 Answers 5

1

$_POST reads data that has been submitted in the request. The values of your inputs will not be available there until after the page has been sent to the browser and a new page has been requested by the submission of a form (with method="post") or some other technique to make an HTTP request (e.g. Ajax) has been used.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use this.

 <?php

 if(isset($_POST['submit'])){

$SERVER_IP = $_POST['checkip'];
$SERVER_PORT = $_POST['checkport'];
echo "server ip is:" .$SERVER_IP;
 }

  ?>

<form method="post" action="">
<input id="checkip" name="checkip" value="217.198.136.31">
<input id="checkport" name="checkport" value="25565">
<input type="submit" value="submit" name="submit"/>
</form>

Comments

0
<input id="checkip" name="checkip" value="217.198.136.31">

is a html-input-element belonging to a FORM, when this form is beeing sent, the target gets the form-fields and these are accessible by $_POST ... but only if the method of the form is POST

example:

<html>
<Body>
<?php
if(isset($_POST['test'])) echo $_POST['test'];
?>
<form action='?' method='post'>
<Input Name='test' value='123'>
<button type='submit'>Send</button>
</form>
</html>

Keep in mind, that php is executed on Server, BEFORE Any form element gets evaluated by the Clients browser

Comments

0

the problem is your not passing the value, the input type field must used inside a form example:

<form method=POST>
 <input id="checkip" name="checkip" value="217.198.136.31">
<input id="checkport" name="checkport" value="25565">
<input type="submit" value="Submit" />
</form>

cheers !

Comments

0

Please make sure that you are actually posting your form.

$_POST will get values only if the form is posted with method="post".

Otherwise, your code seems fine.

Hope it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.