1

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';
        }
4
  • Use AJAX or work with PHP only. Commented Oct 4, 2017 at 11:32
  • 1
    He doesn't need to use AJAX since he wants to move to another page. He can simply pass the variable in query component of URI. Commented Oct 4, 2017 at 11:36
  • stackoverflow.com/questions/11480763/… Here what @Walk said Commented Oct 4, 2017 at 11:37
  • thank you for your help.....this is a very useful site Commented Oct 6, 2017 at 6:14

5 Answers 5

1

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';
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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'];

Comments

0

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.

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.