Ankur Choudhury
Ankur Choudhury

Reputation: 189

JavaScript values to another PHP page

i m 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 i m having is sending a value to the other page the code on 1st page is

     <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=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';
        }

Upvotes: 1

Views: 113

Answers (5)

farshad noruzi
farshad noruzi

Reputation: 54

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

$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';
        }

Upvotes: 1

Farhang Negari
Farhang Negari

Reputation: 229

change html to :

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

$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';
                }

Upvotes: 0

Suvethan Nantha
Suvethan Nantha

Reputation: 2454

Try this

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>

Upvotes: 0

Cripi
Cripi

Reputation: 53

Ajax is the solution for you.

Plain ajax looks like 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();

I would recommend using the jquery ajax, because it's way more simple and beginner friendly.

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"]. 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.

Upvotes: 0

Yaroslav  Mironyuk
Yaroslav Mironyuk

Reputation: 51

U may use get parameters of url, for example:

<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 = $_GET['q'];

Upvotes: 0

Related Questions