2015-10-06 32 views
-4

我有一個PHP頁面內是像這樣的警告:是否可以在<script>中放置一部分PHP代碼?

<script> 
alert('Stop the timer!'); 
</script>"; 

現在我想試試/做到這一點:

<script> 
<?php echo "$test2"; ?> 
alert('Stop the timer!'); 
</script> 

(我嘗試做的是回饋$ test2的內容通過警報的方式...)

現在,這是行不通的。但是......它也不會給出錯誤信息。

所以這裏2分的問題是:

1)它甚至有可能把PHP的一部分,在部分 2)如果它不工作,爲何它沒有給出錯誤信息 (3 )我必須使$ test2全球? (以及如何更好地格式化,如果是的話)

在此先感謝。

<!DOCTYPE html> 
     <head> 
     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
     <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
     <!-- deze hieronder is nodig voor touchpad enabled sliders --> 
     <script src="jquery.ui.touch-punch.min.js"></script> 
     <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
     <link rel="stylesheet" href="/resources/demos/style.css"> 




     <script> 
    function getSelectValues(select) { 
     var result = []; 
     var options = select && select.options; 
     var opt; 

     for (var i=0, iLen=options.length; i<iLen; i++) { 
     opt = options[i]; 

     if (opt.selected) { 
      result.push(opt.value || opt.text); 
     } 
     } 
     return result; 
    } 
     </script> 

     <!-- accordion --> 
     <script> 
     $(function() { 
     $("#accordion").accordion({ 
      event: "click", 
      active: false, 
      collapsible: true, 
      autoHeight: false 

     }); 
     }); 
     </script> 

     <script> 
     $(function() { 
     $("#slider-vertical").slider({ 
      orientation: "horizontal", 
      range: "min", 
      min: 0, 
      max: 100, 
      value: 1, 
      slide: function(event, ui) { 
      $("#amount").val(ui.value); 
      } 
     }); 
     $("#amount").val($("#slider-vertical").slider("value")); 
     }); 
     </script> 
     <script> 
     $(function() { 
     $("#slider-vertical2").slider({ 
      orientation: "horizontal", 
      range: "min", 
      min: 0, 
      max: 100, 
      value: 1, 
      slide: function(event, ui) { 
      $("#amount2").val(ui.value); 
      } 
     }); 
     $("#amount2").val($("#slider-vertical2").slider("value")); 
     }); 
     </script> 
     <title>Left X Right -BrainGame</title> 
     <!-- Bootstrap --> 
     <link href="css/bootstrap.css" rel="stylesheet"> 
      <script src="js/bootstrap.min.js"></script> 

      <script> 
     $(function() { 
     $("#datepicker").datepicker(); 
     }); 
     </script> 
     <script> 
     $(".js-example-basic-multiple").select2(); </script> 

    <style type="text/css"> 
    p.label_checkbox_pair { 
    clear: both; 
    float: none; 
    position: relative; 
    } 
    p.label_checkbox_pair input { 
    left: 80px; 
    position: absolute; 
    top: 1px; 
    } 
    p.label_checkbox_pair label { 
    display: block; 
    margin-left: 90px; 
    width: 200px; 
    } 
    </style> 

    <script src="js/icheck.js"></script> 
    <script> 
    //oud $(document).ready(function(){ 
     //$('input').iCheck({ 
     //checkboxClass: 'icheckbox_polaris', 
     //radioClass: 'iradio_polaris', 
    // 


     //increaseArea: '20%' optional 
     //}); 
    //}); 
    $(document).ready(function(){ 
     $('input').each(function(){ 
     var self = $(this), 
      label = self.next(), 
      label_text = label.text(); 

     label.remove(); 
     self.iCheck({ 
      checkboxClass: 'icheckbox_line-blue', 
      radioClass: 'iradio_line-blue', 
      insert: '<div class="icheck_line-icon"></div>' + label_text 
     }); 
     }); 
    }); 
    </script> 
     <link href="skins/line/blue.css" rel="stylesheet"> 


     </head> 

    <BODY> 
    <br /><br /> 
    <table HSPACE="50" CELLPADDING="20"> 
    <tr HSPACE=50 CELLPADDING="30"> 
    <td> 
    <?php 
    $a=array("red","blue","black","orange","green"); 
    $random_keys=array_rand($a,5); 


    $textcolours=array('red','blue','black','orange','green'); 
    shuffle($textcolours); 

    echo "<right><font size=6 color='$textcolours[0]' >".$a[$random_keys[0]]."<br><br></font></right>"; 
    echo "<right><font size=6 color='$textcolours[1]' >".$a[$random_keys[1]]."<br><br></font></right>"; 
    echo "<right><font size=6 color='$textcolours[2]' >".$a[$random_keys[2]]."<br><br></font></right>"; 
    echo "<right><font size=6 color='$textcolours[3]' >".$a[$random_keys[3]]."<br><br></font></rightr>"; 
    echo "<right><font size=6 color='$textcolours[4]' >".$a[$random_keys[4]]."<br><br></font></right>"; 
    ?> 
    </td> 

    <td> 

     <div class="left"> 

     <form class="span4" action="" method="post"> 

     <fieldset> 

     <legend>Left X Right BrainGame</legend> 

     <p halign="right" margin="10px">Name: <input type="text" name="naam" ></p> 

     <p halign="right" margin="10px">Date: <input type="text" name="date" id="datepicker" ></p> 

    <br> 

      <?php 
    if(isset($_POST['getdata'])){ 
    $conn=mysql_connect('localhost','t','w'); 
    mysql_select_db("test",$conn); 

    $regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/"; 

    $naam=$_POST['naam']; 
    $date=$_POST['date']; 
    //$type=$_POST['type']; 
    //$core=$_POST['core']; 
    //$management=$_POST['management']; 


    //$names = $tools; 
    //$output = preg_grep('/(Andrew|John)/i', $names); 
    //print_r($output); 

    // 
    //http://webcheatsheet.com/php/regular_expressions.php#match 
    $test = $_POST['tools']; 
    $result = preg_replace('/({|:|true|}|")/', '', $test); 
    $test2 = preg_grep('/(^RED|^BLACK|^BLUE|^ORANGE|^GREEN|^red|^black|^blue|^orange|^green)/i', $result); 

    $tools = json_encode($test2); 
    //$tools = $_POST['tools']; 
    //$tools = json_encode(preg_grep('/(RED|BLACK|BLUE|ORANGE|GREEN)/i', $_POST['tools'])); 
    //$tools = json_encode($_POST['tools']); 
    //$analytisch=$_POST['analytisch']; 
    //$eneagram=$_POST['eneagram']; 

    if(true == false){ 
     echo "<label class='err'>All fields are required</label>"; 
     } 

      else{ 
    $insert="Insert into kandidaat(naam,date,tools) 
    values('".$naam."','".$date."','".$tools."')"; 

    $rs=mysql_query($insert) or die(mysql_error()); 

    ?> 
    <script> 
    <?php echo "$test2"; ?> 
    alert('Stop the timer!'); 

    </script>"; 
    <?php } 
    } 
    ?> 

     </fieldset> 


    <div id="accordion" style="width:90%;"> 
     <h3>Start...</h3> 
     <div> 
     <p class="label_checkbox_pair"> 

    <!--<div style="font-family: arial; font-size: 6px; color: #0B1DE0; vertical-align: middle;"><input style="vertical-align: middle" type="checkbox" name="iCheck" value="true"><label for="certs">ISTQB2</label></div>--> 
    <div style="font-family: arial; font-size: 6px; color: #0B1DE0; vertical-align: middle;"><input style="vertical-align: middle" type="checkbox" name="certs[RED]" value="true"><label>RED</label></div> 
    <div style="font-family: arial; font-size: 6px; color: #0B1DE0; vertical-align: middle;"><input style="vertical-align: middle" type="checkbox" name="certs[BLUE]" value="true"><label>BLUE</label></div> 
    <div style="font-family: arial; font-size: 6px; color: #0B1DE0; vertical-align: middle;"><input style="vertical-align: middle" type="checkbox" name="certs[BLACK]" value="true"><label>BLACK</label></div> 
    <div style="font-family: arial; font-size: 6px; color: #0B1DE0; vertical-align: middle;"><input style="vertical-align: middle" type="checkbox" name="certs[ORANGE]" value="true"><label>ORANGE</label></div> 
    <div style="font-family: arial; font-size: 6px; color: #0B1DE0; vertical-align: middle;"><input style="vertical-align: middle" type="checkbox" name="certs[GREEN]" value="true"><label>GREEN</label></div> 
     </p> 
     </div> 

     <h3>Part 2</h3> 
     <div> 
     <p> 
     <!-- zie content website https://select2.github.io/examples.html --> 
     <fieldset> 
    <div><input type="checkbox" name="tools[RED]" value="true"><label>RED</label></div> 
    <div><input type="checkbox" name="tools[BLUE]" value="true"><label>BLUE</label></div> 
    <div><input type="checkbox" name="tools[BLACK]" value="true"><label>BLACK</label></div> 
    <div><input type="checkbox" name="tools[ORANGE]" value="true"><label>ORANGE</label></div> 
    <div><input type="checkbox" name="tools[GREEN]" value="true"><label>GREEN</label></div> 
    </fieldset> 
    </p> 
     </div> 

    </div> 


    <br/> <button type="submit" name="getdata" class="btn">Submit</button> 
    <a href="toonkandidaat.php" class="btn btn-primary">Highscores</a> 


    </form> 

     </div> 
     <?php 
     function save(){ 

     } 
     ?> 
    <?php 
    function make_links_clickable($text){ 
     return preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Z&#1072;-&#1103;&#1040;-&#1071;()[email protected]:%_+.~#?&;//=]+)!i', '<a href="$1">$1</a>', $text); 
    } 
    ?> 

    </td> 
    </tr> 
    </table> 

     </body> 
     </html> 
+2

你爲什麼不走並自己嘗試一下? –

+0

就像我說的。我沒有看到任何事情發生......我想從事實知道它是否可以在嘗試查看不可能存在的東西之前工作:) – tijntest

+0

它應該工作。如果您在客戶端的腳本部分找不到'$ test2'的值,那意味着'$ test2'值是空的。無論如何,這不會提醒'$ test2'的值,因爲您只是將它打印在腳本部分中,並且什麼也不做。 –

回答

1

您的語法錯誤。想象一下,你已經在你的varibale中保存了foobar。 如果您像您一樣打印,結果將會是 foobar

但是,您將它打印在腳本部分中,因此您需要將它分配給JavaScript變量。有兩種可能性來提醒它。

alert("<?php echo $test; ?>"); 

或者

var content = "<?php echo $test; ?>"; 
alert(content); 
+0

它給了回數組....所以我試着.. alert(「<?php echo json_encode($ test2);?>「);它返回的結果與在html表中一樣... [] – tijntest

0

是的,這是可能的,因爲這樣的事實:PHP腳本由服務器首先在PHP解釋器進行評估,提供普通的HTML/CSS/JS到客戶端瀏覽器。

不管怎麼說,只要自己嘗試一下,就會更快。

爲了回答它爲什麼在你的情況下不起作用,我們需要更多的代碼來查看你的變量是如何以及在哪裏定義的。

+0

是這樣的可能嗎? $ tools = json_encode(preg_grep('/(RED | BLACK | BLUE | ORANGE | GREEN)/ i',$ _POST ['tools'])); ___如果是這樣,如何正確格式化? – tijntest

1

試試這個?

<script> 
alert('<?php echo $test2; ?>'); 
</script> 

alert('<?php echo $test2; ?>'); 

會被你的PHP變量(由例如 「你好詞」)的內容替換,就像你寫了這個:

alert('Hello word'); 
+0

Hey Simon,ben je Nederlander?我是PHP新手,非常抱歉我的很多錯誤。 。:數組順便說一句,我特別爲你添加了代碼,並且順便說一下,它正在構建一個小型的腦遊戲。 ik je laten zien wat ik tot nu toe heb。 – tijntest

+0

Re,哈哈不,我不是荷蘭人說的:)我來自比利時,法國方面,但像很多比利時人,我有一個荷蘭名字...... :) 也許你的PHP變量的內容不是一個字符串,而是一個數組? –

相關問題