2017-01-30 59 views
0

我已經做了一個簡單的形式和PHP腳本,應該從html表單文本框中讀取一個變量,當用戶點擊提交一個簡單的消息時,會顯示說'你正在搜索歌曲通過artist_name'但是當我嘗試我得到一個空白頁面或消息說未定義。但是,如果我只是回聲的PHP變量它顯示正確的值。php警報沒有定義或沒有顯示

我試圖只使用

alert($artist_name) and alert('$artist_name') 

,但我得到未捕獲的ReferenceError:未定義$這位演出。或者警報顯示'$ artist_name'而不是值?

但是像

<?php echo $_GET["artist"]; ?> 

順利拿到文本???

這個工作也沒關係。

elseif ($artist_name =="foo") { ?> 
    <script language="javascript" type="text/javascript"> 
     alert("you enetered foo"); 
     window.location = 'index.html'; 
    </script> 
<?php 
} 

所以越來越變量確定,但我只是不能似乎把它列入我的警報消息是這段代碼的整個目的,請能有人告訴我什麼,我做錯了。非常感謝。

我下面的代碼:

的HTML

<form id="form1" action="searchresults.php" method="GET"> 
    <div id="artform"> 
     <fieldset> 
      <legend> Search for music </legend> 
      <p> <label> Artist (*): </label> <input type="text" name="artist" id="artist" placeholder="enter an artists" class="add1"></p> 
     </fieldset>        
     <input type="submit" class="button1"> 
     <input type="reset" class="button2"> 
    </div> 
</form> 

php的

<?php 
$artist_name = $_GET['artist']; 

if (empty($artist_name)) { ?> 
    <script language="javascript" type="text/javascript"> 
     alert('Field blank !, please try again.'); 
     window.location = 'index.html'; 
    </script> 
<?php 
} 
else { ?> 
    <script language="javascript" type="text/javascript"> 
     alert('You are searching for songs by' $artist_name); // the issue is here 
     window.location = 'index.html'; 
    </script> 
<?php 
} 
?> 
+1

'alert('您正在搜索歌曲<?= $ artist_name?>'';' – 1252748

回答

2

你忘了附上$這位演出用PHP開始和結束標記。

alert('You are searching for songs by' $artist_name); // the issue is here 

不該得到:

alert('You are searching for songs by <?php echo $artist_name; ?>'); // the issue is here 
+1

結束引用是錯誤的地方 – 1252748

+0

您是對的,謝謝@ 1252748 – KEK

+1

重要的是要注意的問題是,從根本上說,他需要回顯該變量的值而不是打印變量名稱。沒有echo語句的PHP標籤不會產生所需的結果。 Edwin,要打印變量的「解釋」/實際值,您需要1)將代碼作爲@KEK記錄在PHP標記中,2)「echo」/打印值 –

0

謝謝你,我懂了工作,乪我想你的方法,但我得到一個錯誤未捕獲的語法錯誤:之後參數列表丟失),但是125248意見非常完美,我不得不包含標籤才能正常工作。感謝您的時間。