2017-10-04 43 views
1

我嘗試通過使用Java腳本,用戶會被重定向到另一個PHP頁面oncick,從一個頁面發送一個值到另一個 問題IM已經發出了一個值到另一個頁面 第1頁上的代碼被的JavaScript值到另一個PHP頁面

 <html> 
<body> 
    <div id="management" onclick="myFunction()" class="col-md-2"> 

        <p>Management</p> 

    </div> 
<script> 
function myFunction() { 
    var search="Assam"; 
    location.href = "search.php"; 
} 
</script> 

</body> 
</html> 

,我想搜索的價值將被轉發到第二頁的search.php

  $search=how do i get the variable here; 
$query = $pdo->prepare("select * from collegetable where name LIKE '%$search%' OR courses LIKE '%$search%' OR address LIKE '%$search%' OR affiliation LIKE '%$search%' LIMIT 0 , 10"); 
$query->bindValue(1, "%$search%", PDO::PARAM_STR); 
$query->execute(); 
// Display search result 
     if (!$query->rowCount() == 0) { 
       echo "Search found :<br/>"; 
       echo "<table style=\"font-family:arial;color:#333333;\">"; 
       echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">College Names</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Courses</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>";    
      while ($results = $query->fetch()) { 
       echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";    
       echo $results['name']; 
       echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">"; 
       echo $results['courses']; 
       echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">"; 
       echo $results['fees']; 
       echo "</td></tr>";    
      } 
       echo "</table>";   
     } else { 
      echo 'Nothing found'; 
     } 
+0

使用AJAX或僅PHP工作。 – Twinfriends

+1

他不需要使用AJAX,因爲他想移動到另一個頁面。他可以簡單地在URI的查詢組件中傳遞變量。 – Walk

+0

https://stackoverflow.com/questions/11480763/how-to-get-parameters-from-a-url-string這裏什麼@Walk說 – Doomenik

回答

2

使用查詢字符串着第二頁

<html> 
<body> 
    <div id="management" onclick="myFunction()" class="col-md-2"> 

     <p>Management</p> 

    </div> 
<script> 
function myFunction() { 
    var search="Assam"; 
    location.href = "search.php?q=" + search; 
} 
</script> 

</body> 
</html> 

,並在第二頁的URL獲得q

$search= $_GET['q']; 
$query = $pdo->prepare("select * from collegetable where name LIKE '%$search%' OR courses LIKE '%$search%' OR address LIKE '%$search%' OR affiliation LIKE '%$search%' LIMIT 0 , 10"); 
$query->bindValue(1, "%$search%", PDO::PARAM_STR); 
$query->execute(); 
// Display search result 
     if (!$query->rowCount() == 0) { 
       echo "Search found :<br/>"; 
       echo "<table style=\"font-family:arial;color:#333333;\">"; 
       echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">College Names</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Courses</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>";    
      while ($results = $query->fetch()) { 
       echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";    
       echo $results['name']; 
       echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">"; 
       echo $results['courses']; 
       echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">"; 
       echo $results['fees']; 
       echo "</td></tr>";    
      } 
       echo "</table>";   
     } else { 
      echo 'Nothing found'; 
     } 
+0

感謝你的幫助非常有用的網站.....這是一個非常有用的網站 –

0

變化的html:

 <html> 
<body> 
    <div id="management" onclick="myFunction()" class="col-md-2"> 

        <p>Management</p> 

    </div> 
<script> 
function myFunction() { 
    var search="Assam"; 
    location.href = "search.php?search="+search; 
} 
</script> 

</body> 
</html> 

PHP來:

$search = $_GET['search']; 
     $query = $pdo->prepare("select * from collegetable where name LIKE '%$search%' OR courses LIKE '%$search%' OR address LIKE '%$search%' OR affiliation LIKE '%$search%' LIMIT 0 , 10"); 
     $query->bindValue(1, "%$search%", PDO::PARAM_STR); 
     $query->execute(); 
     // Display search result 
       if (!$query->rowCount() == 0) { 
         echo "Search found :<br/>"; 
         echo "<table style=\"font-family:arial;color:#333333;\">"; 
         echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">College Names</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Courses</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>";    
        while ($results = $query->fetch()) { 
         echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";    
         echo $results['name']; 
         echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">"; 
         echo $results['courses']; 
         echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">"; 
         echo $results['fees']; 
         echo "</td></tr>";    
        } 
         echo "</table>";   
       } else { 
        echo 'Nothing found'; 
       } 
+0

感謝你的幫助.....這是 –

0

U可以使用get參數URL,例如:

<script> 
function myFunction() { 
    var search="Assam"; 
    location.href = "search.php?q=" + search; 
} 
</script> 

要獲得的search.php參數使用$ _ GET方法http://php.net/manual/en/reserved.variables.get.php

$search = $_GET['q']; 
0

Ajax是你的解決方案。

平原AJAX看起來像this

var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
    document.getElementById("demo").innerHTML = this.responseText; 
    } 
    }; 
    xhttp.open("GET", "ajax_info.txt", true); 
    xhttp.send(); 

我會建議使用jquery ajax,因爲它的方式更簡單,初學者友好。

爲您的使用情況的一個例子是這樣的:

<script> 
var search="Assam"; 
$.ajax({ 
    method: "GET", 
    url: "some.php?search=" + urlencode(search) 
}) .done(function(response) { 
    $("#field-for-response").html(response); 
    }); 
</script> 

在PHP中,你可以讀了$_GET["search"]值。如果你只是想找到剛剛PHP頁面上的客戶端,你應該有this一看,卻阿賈克斯給你的優勢無需重新加載頁面,這是什麼使用戶體驗更加順暢。

0

試試這個

的Javascript

$scope.submitForm = function (form, e) { 
    if(form.$valid){ 
     // e.preventDefault(e); 
     $http({ 
      method : "POST", 
      url : "search.php", 
      data: { 
       "givenName":"james", 
       "displayName":"Cameroon" 
      }, 
      headers: { 
       "Access-Control-Allow-Origin": "*", 
       "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", 
       "Access-Control-Allow-Headers": "Content-Type, X-Requested-With", 
       "Content-Type": "application/json" 
      }}).then(function(response) { 
       console.log(response); 
     }, function(response) { 
      console.log("Error"+response); 
     }); 
    } 
} 

HTML

<form id="attributeVerification" name="vm.attributeVerification" onsubmit="submitForm(vm.attributeVerification)" novalidate> 
    <div class="attr" id="attributeList"> 
     <ul> 
      <li> 
       <div class="attrEntry"> 
        <label for="givenName">First name</label> 
        <div class="helpText" role="alert" aria-live="polite" tabindex="1">This information is required.</div> 
        <input id="givenName" name="givenName" class="textInput" type="text" placeholder="First name" title="Your given name (also known as first name)." required maxlength="15" ng-model="userInfo.givenName" aria-required="true"> 
       </div> 
      </li> 
      <li> 
       <div class="attrEntry"> 
        <label for="displayName">Last name</label> 
        <div class="helpText" role="alert" aria-live="polite" tabindex="1">This information is required.</div> 
        <input id="displayName" name="displayName" class="textInput" type="text" placeholder="Last name" title="Your display name." required maxlength="25" ng-model="userInfo.displayName" aria-required="true"> 
       </div> 
      </li> 
     </ul> 
    </div> 
    <div class="buttons"> 
     <button id="continue" aria-label="Create" type="submit">Continue</button> 
    </div> 
</form> 
相關問題