/ / PHP echo Zmienna wejściowa HTML poza heredoc - javascript, php, html

PHP echo zmienna wejściowa HTML poza heredoc - javascript, php, html

Umieszczam mój kod HTML w PHP za pomocą heredoc, a jachcesz pobrać zmienną wejściową użytkownika, która znajduje się wewnątrz heredoc, i odtworzyć ją echo. Próbowałem użyć $ _GET ["wejście"], ale pojawił się błąd undefined index: input Czy mogę wiedzieć, jak uzyskać zmienną wejściową?

<?php
$htmlfile= <<<html
<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
<title>Seller Evaluation System</title>
<style type="text/css">
body {background-image:url("images.jpg");}
</style>
</head>

<body>
<h1><center><font color="darkblue">Seller Evaluation System</font><center></h1>

<p><center>
<script>
function searchSite(){
var input=document.getElementById("searchinput").value;
var searchForm=document.getElementById("searchForm");
searchForm.action="http://www.mudah.my/Malaysia/Electronics-3000/"+input+"-for-sale?lst=0&fs=1&q="+input+"y&cg=3000&w=3&so=1&st=s";
searchForm.submit();
}
</script>

<form method="get" action="ttt.php" id="searchForm">
<input type="text" id="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>
<button onclick="searchSite()">Search</button>
</form>

<p><label>Mudah<input type="checkbox" name="searchFrom" value="Mudah" checked/></label>
<label><font color="grey">Lazada</font><input type="checkbox" name="searchFrom" value="Lazada" disabled="disabled"/></label>
<label><font color="grey">Lelong</font><input type="checkbox" name="searchFrom" value="Lelong" disabled="disabled"/></label>
<label><font color="grey">Ebay</font><input type="checkbox" name="searchFrom" value="Ebay" disabled="disabled"/></label></p>
</center></p></br>
</body>
</html>
html;

echo $htmlfile;

$userInput=$_GET["input"];
echo $userInput;
?>

Odpowiedzi:

0 dla odpowiedzi № 1

Musisz nazwać nazwę wejściową „searchinput”. Ale nie może być dostępny, jeśli nie wyślesz formularza i musisz nazwać go tagiem „nazwa”, a nie „id”.

Możesz go wyświetlić po wysłaniu za pomocą JavaScript, bez PHP.

To działa, ale formularz musi być przetwarzany przez ten sam plik, więc „akcja” musi być pusta. Jeśli nie, możesz pobrać parametry GET w pliku przetwarzającym formularz, w przykładowym ttt.php

<?php
$htmlfile= <<<html
<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
<title>Seller Evaluation System</title>
<style type="text/css">
body {background-image:url("images.jpg");}
</style>
</head>

<body>
<h1><center><font color="darkblue">Seller Evaluation System</font><center></h1>

<p><center>



<form method="get"  id="searchForm">
<input type="text" name="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>
<button type="submit">Search</button>
</form>


</center></p></br>
</body>
</html>
html;

echo $htmlfile;

$userInput=$_GET["searchinput"];
echo $userInput;
?>

0 dla odpowiedzi nr 2

$_GET[..] bierze Nazwa elementu formularza. Więc w swoim kodzie HTML musisz uwzględnić to:

<input type="text" name="input" id="searchinput" size="33" placeholder="Search Electronic Gadgets..." autofocus>

Pamiętaj również, że chcesz otrzymać przedmiot tylko po przesłaniu formularza, musisz najpierw sprawdzić, czy GET istnieje:

// If we submitted our form with a element named "input", then echo
if(isset($_GET["input"]) {
$userInput=$_GET["input"];
echo $userInput;
}

W ten sposób kod będzie działał tylko wtedy, gdy formularz zostanie przesłany.