2014-09-17 2 views
0

Я попытался сделать калькулятор с квадратным корнем с PHP и HTML, используя форму. но он, похоже, не получит выходной оператор. Вот оно:PHP Square Root Calculator (w/HTML)

<?php 
$num = $_POST['getroot']; 
$pull_sqrt = print(sqrt($num)); 
print("The Square root of "$num"is "$pull_sqrt); 
?> 

<form action="root.php" method="post"> 
<input type="text" id="getroot" value="Number"/> 
<input type="submit" id="submitroot" value="Calculate"/> 
</form> 

для root.php:

<?php 
$num = $_POST['getroot']; 
$pull_sqrt = print(sqrt($num)); 
print("The Square root of "$num"is "$pull_sqrt); 
?> 

Пожалуйста, помогите мне на объясняя это, я до сих пор не знаю, если PHP разрешено SQRT(); как функция больше. Любой способ переконфигурировать в порядке, я хотел бы объяснить способ исправления этого. Благодаря!

+0

Что говорит журнал ошибок php? – Sinkingpoint

+0

@Quirliom, "Неопределенный пост, установленный из '$ pull_sqrt' expected '{'" – user3648388

+0

@ user3648388 Вам не хватает некоторых операторов конкатенации '.'. – Brad

ответ

2

Вы не имеете элемент формы с именем getroot

Вы хотите сделать <input type="text" id="getroot" name="getroot" value="Number"/>

Вы не можете полагаться только на id. POST требует элемент «named».

Вы также отсутствует Сцепляет для print("The Square root of "$num"is "$pull_sqrt);

Sidenote: Удалить print из $pull_sqrt = print(sqrt($num)); в противном случае он будет эхо, как 1.

сделать

print("The Square root of " . $num . "is " .$pull_sqrt); 

Поскольку вы используете это на одной странице, вам необходимо использовать isset() и используя action="".

<?php 

if(isset($_POST['submit'])){ 
$num = $_POST['getroot']; 
$pull_sqrt = sqrt($num); 
print("The Square root of " . $num . " is " . $pull_sqrt); 

} 
?> 

<form action="" method="post"> 
<input type="text" id="getroot" name="getroot" placeholder="Enter a number"/> 
<input type="submit" name="submit" id="submitroot" value="Calculate"/> 
</form> 

Вы также можете проверить, если это на самом деле это число, которое было введено, используя is_numeric().

<?php 

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

    if(is_numeric($_POST['getroot'])){ 
     $num = (int)$_POST['getroot']; 
     $pull_sqrt = sqrt($num); 
     print("The Square root of " . $num . " is " . $pull_sqrt); 

// Yes, you can do the following: 
$pull_sqrt = print($num * $num); // added as per a comment you left, but deleted. 
} 

else{ 
echo "You did not enter a number."; 
} 

} 
?> 

<form action="" method="post"> 
<input type="text" id="getroot" name="getroot" placeholder="Enter a number"/> 
<input type="submit" name="submit" id="submitroot" value="Calculate"/> 
</form> 
+0

Возможно, вы можете установить его в форме php? – user3648388

+0

Вам также не хватает конкатенаций, как указано в комментариях. @ user3648388 –

+0

@ user3648388 Добро пожаловать. –