1

This is my first project where I used Jquery. There are two pages 1. listofleaders.php 2. leadersprofile.php

On First Page i.e. listofleaders.php I have a input text box, where user enters leaders name and I used jQuery code to transfer textbox values to leaderprofile.php page

    <html>
    <head>
    <script>
      function ls() 
        {
        var leaderdetails = "leaderprofile.php?lname="+$("#gopal").val();
        $.get(leaderdetails, function( data ) {
        //alert(leaderdetails);
        location.href = "leaderprofile.php";
        });
        }   
    </script>
    </head>
    <body>
    <input type="text" id="gopal" name="t"  placeholder="Start Typing" size="50" />
<button onclick="ls();" type="button">Go!</button><br><br>
    </body>
    </html>

On Second Page leadersprofile.php I have written this,

<?php
include "admin/includes/dbconfig.php";
$lname = $_GET['lname'];
echo $lname;
?> 

But on second page i.e. leaderprofile.php it is showing me error Undefined index : lname

Am I Correct to this approach ? Where I am Wrong ? Hope you Understand.

5
  • location.href = "leaderprofile.php"; redirects you to leaderprofile.php without parameters. What do you expect? Commented Jul 28, 2015 at 18:44
  • I want to go to leaderprofile.php with parameters what shouls i write there? Commented Jul 28, 2015 at 18:50
  • location.href = "leaderprofile.php?parameter=value";? Commented Jul 28, 2015 at 18:51
  • Do i have to remove var leaderdetails = "leaderprofile.php?lname="+$("#gopal").val(); Commented Jul 28, 2015 at 18:54
  • 1
    I don't know what you should do with your code. Commented Jul 28, 2015 at 18:55

3 Answers 3

2

So I am having a guess here at what you are trying to achieve based on your problem description.

If you want to send a <input> value to another page, you better use a classic POST request (without the need of evolving jQuery):

<form method="post" action="leadersprofile.php">
    <input type="text" name="lname"/>
    <button type="submit">Send</button>
</form>

And in leadersprofile.php:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['lname'])) {
    $lname = $_POST['lname'];
    var_dump($lname); // outputs whatever the user input was
}

Now if you want to send the data to leadersprofile.php without reloading the page, you are looking for an Ajax request (XmlHttpRequest).

jQuery(function($) {
    $('form').on('submit', function(e) {
        e.preventDefault(); // prevents default behavior that is submitting the form
        $.ajax({
            method: 'post', // can be also 'get'
            url: 'leadersprofile.php',
            data: {lname: $('input').val() },
            success: function(html) {
                $('div').html(html); // place whataver was printed in leadesrprofile.php into a div
            },
            error: function(r) { // fire if HTTP status code != 200
                console.log(r);
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You seem to be using JQuery correctly. The Javascript to extract the value and the send the GET request should be working.

Your misunderstanding lies in how you check if the PHP file has received the request. This redirect

location.href = "leaderprofile.php";

Will not provide you any information about the GET request that you just made. Instead you can try:

location.href = "leaderprofile.php?lname=" + $("#gopal").val()

To verify that your PHP and Javascript is performing as expected. If you see the values that you expect then I believe you have confirmed two things:

  • successfully extracted the correct value from the textbox
  • GET request is succeeding, and the success callback is being invoked

Comments

0

I understand your question.Try the following codes.

listofleaders.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form>
        <table>
            <tr>
                <td>Name:</td>
                <td><input type="text" id="name"></td>
            </tr>
            <tr>
                <td></td>
                <td><button id="submit">Submit</button></td>
            </tr>
        </table>
    </form>
    <script src = "jquery.js"></script>
    <script src = "leader.js"></script>
</body>
</html>

When submit button is click, leader.js file will get the value of text box.

leader.js

$(document).ready(function() {
    $('#submit').on('click', function(){

        var name = $('#name').val();

        $.ajax({
            url:'leaderprofile.php',
            type:'POST',
            data:{'name':name},
            success:function(data){

            }
        });
    });

});

Now, this leader.js file will send the name key to liderprofile.php.

After that php file witt return the data(name) to js file..and the js file will alert name.

leaderprofile.php

<?php

$name = $_POST['name'];
echo $name;

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.