2017-07-30 45 views
-1

你好我試圖使用PHP創建動態網頁如何使動態的形式作用於PHP

當有人訪問我的網站,它會顯示輸入文本框爲他們進入

喜歡這個

enter image description here

enter image description here

在文本框中輸入自己的名字後,就應該給我

隨機圖像作爲輸出像#此

enter image description here

我都做過類似的這種http://cocfun.ml/一個網站,但我想同樣喜歡rubie.me

這裏我主持我的測試網站供您參考

http://nametest.byethost9.com/

我希望你明白我的要求......任何人都可以幫我解決問題。

<center> 
 
</br></br></br></br> 
 
<?php 
 
    if(isset($_POST['submit'])){ 
 
     $userInput = $_POST['userInput']; 
 
     header('Location:/'.$userInput); 
 
    } 
 
?> 
 

 
<form action="display()" method="POST"> 
 
<input type="text" class="form-control input-lg" placeholder="Your Name?" name="userInput" style="width:80%;"> 
 
</br> 
 
&nbsp 
 
&nbsp 
 
<input type="submit" class="btn btn-primary btn-lg" name="submit" value="See Result"></form> 
 
    
 

 
    
 

 
<?php 
 
function display() 
 
{ 
 
\t include'img.php'; 
 
}     \t \t \t \t 
 
if(isset($_POST['submit'])) 
 
{ 
 
    display(); 
 
} 
 
?> 
 
<?php 
 
include'img.php'; 
 
?>

請當我運行這唯一的形式輸入顯示不是圖像

回答

0

我不能完全肯定我理解了問題,但我認爲你可以做到這一點?如果表單只能在第一次提交表單後使用會話變量來控制表單是否顯示時才提交。您的代碼似乎表明,用戶可以輸入目的地userInput和形式會使用,因爲它是行動〜所以畫面將永遠不會看到,除非用戶返回頁面〜在這種情況下會,如果他們需要一個會話變量是不是再次看到表格。

<?php 
    if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['userInput'])){ 
     /* Form has been submitted, show image */ 
     include'img.php'; 
    } else {/* open else condition */ 
?> 

<form method='post'> 
    <input type='text' class='form-control input-lg' placeholder='Your Name?' name='userInput'> 
    <input type='submit' class='btn btn-primary btn-lg' name='submit' value='See Result'> 
</form> 

<?php 
    }/* close if/else condition */ 
?> 

看到的圖像的例子或許下面可能幫助後?

<?php 
    session_start(); 

    $field='userInput'; 

    if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST[ $field ])){ 
     $face=str_replace(' ', '_', urldecode(filter_input(INPUT_POST, $field, FILTER_SANITIZE_STRING))); 
     $_SESSION['face']=$face; 

     /* the url structure is setup in htaccess for my dev server, change to suit */ 
     exit(header('Location: /face/'.$face)); 
    } 
?> 
<!doctype html> 
<html> 
    <head> 
     <title>Names and faces</title> 
    </head> 
    <body> 
     <?php 
      if($_SERVER['REQUEST_METHOD']=='GET' && empty($_GET[ $field ])){ 
     ?> 
     <form method='post'> 
      <input type='text' class='form-control input-lg' placeholder='Your Name?' name='userInput'> 
      <input type='submit' class='btn btn-primary btn-lg' name='submit' value='See Result'> 
     </form> 
     <?php 
      } else { 
       $face=filter_input(INPUT_GET, $field, FILTER_SANITIZE_STRING); 
       echo "<img src='/test/face-img.php' />"; 
      } 
     ?> 
    </body> 
</html> 

而且圖像生成器(img.php):

<?php 
    session_start(); 
    ini_set ('gd.jpeg_ignore_warning', 1); 
    if (error_reporting() === 0) return false; 

    /* 
     Directory containing various images from which 
     a random image will be selected. 

     Edit to suit your environment 
    */ 
    $dir = 'c:/wwwroot/images/css/tiles/'; 

    /* 
     Location of a true type font on server 
     Edit to suit your environment. 
    */ 
    $font = 'c:/wwwroot/inc/fonts/arial.ttf'; 


    /* 
     Find images of type `jpg` or `jpeg` 
    */ 
    $files = preg_grep('@(\.jpg$|\.jpeg$)@i', glob($dir . '*.*')); 
    /* 
     find a random file from those in directory 
    */ 
    $file = $files[ array_rand($files, 1) ]; 
    /* 
     get properties of image 
    */ 
    list($w, $h, $t, $a) = getimagesize($file); 

    /* 
     Assign text from session variable 
    */ 
    $text=!empty($_SESSION['face']) ? str_replace('_', ' ', $_SESSION['face']) : 'whoops'; 

    /* 
     fontsize and text angle 
    */ 
    $fontsize=25; 
    $angle=0; 

    /* 
     generate new image and overlay text with shadow 
    */ 
    $img = imagecreatefromjpeg($file); 
    $colour = @imagecolorallocate($img, 255, 255, 255); 
    $shadow = @imagecolorallocate($img, 0, 0, 0); 
    $box = @imagettfbbox($fontsize, $angle, $font, $text); 
    $offset=5; 
    $x = abs($box[0] + (imagesx($img)/2) - ($box[4]/2)); 
    $y = abs($box[1] + (imagesy($img)/2) - ($box[5]/2)); 



    imagettftext($img, $fontsize, $angle, $x+$offset, $y+$offset, $shadow, $font, $text); 
    imagettftext($img, $fontsize, $angle, $x, $y, $colour, $font, $text); 


    header('Content-type: image/jpeg'); 
    imagejpeg($img); 
    imagedestroy($img); 
?> 
+0

它不工作,請參閱該http://nametest.byethost9.com –

+0

這是我的新的託管頁面 https://開頭naveenm106。 000webhostapp.com 我能有你的社交媒體鏈接連接到討論更多關於這一點,如果可能的感謝 –

+0

我還沒有嘗試過,你出我與其他一些登錄就該好好輸出但仍我遇到了一些問題的方法@RamR急救人員 –