简体   繁体   English

JavaScript 值到另一个 PHP 页面

[英]JavaScript values to another PHP page

im trying to send a value from one page to another by using java script, where the user is redirected to the other php page oncick, the problem im having is sending a value to the other page the code on 1st page is我试图通过使用 java 脚本将值从一个页面发送到另一个页面,用户被重定向到另一个 php 页面 oncick,我遇到的问题是向另一个页面发送一个值,第一页上的代码是

     <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>

and i want the value of search to be forwarded to the second search.php page我希望将搜索的值转发到第二个 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';
        }

use query string for forward to second page使用查询字符串转发到第二页

<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>

and in second page get q from URL并在第二页从 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';
        }

change html to :将 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 to : 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';
                }

U may use get parameters of url, for example:你可以使用 url 的 get 参数,例如:

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

To get parameters on search.php use $_GET method http://php.net/manual/en/reserved.variables.get.php要获取 search.php 上的参数,请使用 $_GET 方法http://php.net/manual/en/reserved.variables.get.php

$search = $_GET['q'];

Ajax is the solution for you. Ajax 是您的解决方案。

Plain ajax looks like this :普通的 ajax 看起来像这样

  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();

I would recommend using the jquery ajax , because it's way more simple and beginner friendly.我建议使用jquery ajax ,因为它更简单,初学者友好。

An example for your use case would look like this:您的用例的示例如下所示:

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

In PhP you can read the value over $_GET["search"] .在 PhP 中,您可以通过$_GET["search"]读取值。 If you just want to locate the client just on the php page, you should have a look on this , but Ajax gives you the advantage of no need to reload the page and this is what makes the user experience much smoother.如果你只是想在 php 页面上定位客户端,你应该看看这个,但是 Ajax 给了你不需要重新加载页面的优势,这使得用户体验更加流畅。

Try this尝试这个

Javascript 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM