2015-11-03 77 views
0

這是我第一次在這裏提問。 (對不起,如果我的英語不是那麼好) 我不擅長PHP和HTML。直到上個月,我從來沒有編碼的HTML或PHP。 我是公司的實習生,他們希望我建立一個內聯網。現在一切正常,我已經建立了一個良好的網站使用wordpress,但現在他們問我對我來說太複雜了,沒有人可以幫助我,所以我問你們。在表單上生成新頁面提交數據庫顯示值

在我的頁面上,我有一個表單,在外部數據庫中提交查找,然後將它們顯示在同一頁上。一切正常。我想要的是當用戶輸入輸入值時,我想要生成一個新頁面。現在,當用戶輸入值時,url保持不變(例如:http://localhost/vendors/),但我想要的是當用戶輸入ABCD時,我希望url現在爲http://localhost/vendors/abcd,因此我們可以與顯示的正確數據共享該鏈接頁。

我試圖在互聯網上找到答案,但失敗了。我讀過,我必須使用模板來使用模板生成具有輸入值的新頁面,但我不知道如何做到這一點。我也希望表單在網頁上停留,所以我們可以繼續在數據庫中搜索和顯示其他數據

這裏我有什麼,現在(我已經刪除了所有的問題沒用的東西):

<!--Connection to mssql database--> 
<?php 
    try { 
    $hostname = "hostname"; 
    $port = 1433; 
    $dbname = "databasename"; 
    $username = "username"; 
    $pw = "password"; 
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); 
    } catch (PDOException $e) { 
    echo "Failed to get DB handle: " . $e->getMessage() . "\n"; 
    exit; 
    } 





/** 
* Template Name: Vendors 
* 
* This is the template that displays all pages by default. 
* Please note that this is the WordPress construct of pages 
* and that other 'pages' on your WordPress site will use a 
* different template. 
* 
* @package vantage 
* @since vantage 1.0 
* @license GPL 2.0 
*/ 

get_header(); ?> 






<form action="" method="post"> 
<table id= "formvendor"> 
<tr><td><b>Vendor ID: </b></td><td><input type="text" name="vendorid"></td></tr> 
<tr><td><b>Vendor name: </b></td><td><input type="text" name="vendname"></b></td></tr> 
<input type="submit" 
     style="position: absolute; left: -9999px; width: 1px; height: 1px;" 
     tabindex="-1" /> 
</form> 



<? 
$vendorid = $_POST["vendorid"]; 
$vendname = $_POST['vendname']; 

$vendorid=ltrim($vendorid); 
$vendorid=rtrim($vendorid); 
$vendname=ltrim($vendname); 
$vendname=rtrim($vendname); 


$query="SELECT ... FROM uni.dbo.pm00200 where "; 

if(strlen($vendorid)>0){ 

$query .=" vendorid='$vendorid' or"; 
if(strlen($vendname)>0){ } 
else{ 
$query=substr($query,0,(strLen($query)-3)); 


} 
} 


if(strlen($vendname)>0){ 
$kt2=split(" ",$vendname);//Breaking the string to array of words 
// Now let us generate the sql 
while(list($key,$val)=each($kt2)){ 
if($val<>" " and strlen($val) > 0){$query .= " vendname like '%$val%' or ";} 

      }// end of while 
$query=substr($query,0,(strLen($query)-3)); 
// this will remove the last or from the string. 
     // end of if else based on type value 
} 



foreach ($dbh->query($query) as $t) { 
echo "<table id='tablevendors' style='width:100%' cellpadding='-5px'>"; 
echo '<col width="40%"><col width="60%"'; 
echo "<tr>"; 
echo "<td><b>Vendor ID :</b></td><td> $t[vendorid]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Vendor name:</b></td><td> $t[vendname]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Vendor class:</b></td><td> $t[vndclsid]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Address 1:</b></td><td> $t[address1]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Address 2:</b></td><td> $t[address2]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Address 3:</b></td><td> $t[address3]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>City:</b></td><td> $t[city]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>State:</b></td><td> $t[state]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Zipcode:</b></td><td> $t[zipcode]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Payment:</b></td><td> $t[pymtrmid]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Buyer:</b></td><td> $t[buyer]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Minimum order:</b></td><td> $t[minorder]</td>"; 
echo "</tr><tr>"; 
echo "<td><b>Prepaid info:</b></td><td> $t[prepaid]</td>"; 
echo "</tr>"; 
echo "</table>"; 
} 


unset($dbh); unset($stmt); 

?> 




//rest of template 
<div id="primary" class="content-area"> 

    <div id="content" class="site-content" role="main"> 

     <?php while (have_posts()) : the_post(); ?> 

       <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 

        <div class="entry-main"> 

         <?php do_action('vantage_entry_main_top') ?> 

         <div class="entry-content"> 
          <?php the_content(__('Continue reading <span class="meta-nav">→</span>', 'vantage')); ?> 
          <?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'vantage'), 'after' => '</div>')); ?> 
         </div><!-- .entry-content --> 

         <?php do_action('vantage_entry_main_bottom') ?> 

        </div> 

       </article><!-- #post-<?php the_ID(); ?> --> 

       <?php if (comments_open() || '0' != get_comments_number()) : ?> 
        <?php comments_template('', true); ?> 
       <?php endif; ?> 

      <?php endwhile; // end of the loop. ?> 

</div><!-- #primary .content-area --> 
    </div><!-- #content .site-content --> 


<?php get_footer(); ?> 
+0

看看這個堆棧溢出帖子:http://stackoverflow.com/questions/15236733/pass-form-data-to-another-page-with-php – IndieRok

回答

0

cobstyle。

正如我所看到的,解決方案取決於您希望用戶輸入的內容是否顯示在頁面地址上。如果你想的是,這個問題並不難:

的index.php:

... 
<form action="receiver.php" method="post"> 
    <input type="text" id="txtUserInput" name="txtUserInput" /> 
    <input type="submit" id="btnSubmit" name="btnSubmit" value="Send!" /> 
</form> 

receiver.php:

<?php 
$userInput = ""; 
if (isset($_POST["txtUserInput"])) 
{ 
    $userInput = $_POST["txtUserInput"]; 
    header("Location: show.php?msg=".$userInput.""); //This will redirect your page to another called show.php, with a variable on it 
} 
else 
{ 
    header("Location: error.php"); //Show some error, just in case 
} 
?> 

show.php:

<?php 
if (isset($_GET["msg"])) 
{ 
    echo "<h2>User message: ".$_GET["msg"]."</h2>"; //This will print something on the page 
} 
?> 

我希望你覺得這很有用,但我相信它可以提高很多。 祝你的作品好運,讓我們知道你是否需要更多幫助。

+0

但是,有了這個,我們將能夠分享新的頁面給其他人如http:// localhost/vendors/ABCD? – cobstyle

+0

例如,您將能夠以localhost/vendors.php?msg = ABCD的形式共享它。我希望這是有用的。 –

+0

花了我一些時間,但它的工作!非常感謝 – cobstyle