2

My script fetchs and insert some data from db and displays to the user then I decied to slightly change my code and add some functionality.For example what if any of textbox is empty so that I have added a java script file that controls this sitituation.Bu I cant get the value of textboxes values from index .php file

this is index.php file

<?php    
$records = array();
$db = new mysqli("localhost", "root", "", "app");   
if (!empty($_POST)) {
    $first = $_POST["first"];
    $last = $_POST["last"];
    $bio = $_POST["bio"];    
    $insert = $db->prepare("Insert into people (first,last,bio,created) values(?,?,?,NOW())");
    $insert->bind_param("sss", $first, $last, $bio);
    if ($insert->execute()) {
        header("location: index.php");
    }    
}
if ($result = $db->query("select * from people")) {    
    if ($result->num_rows) {    
        while ($row = $result->fetch_object()) {
            $records[] = $row;
        }
        $result->free();    
    }    
} else {
    $db->error;
}    
?>    
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="sites.js"></script>
    <title>Title</title>
</head>
<body>    
<?php
if (!count($records)) {
    echo "no";
} else {
    ?>
    <table>
        <tr>
            <td>First</td>
            <td>Last</td>
            <td>Bio</td>
            <td>Created</td>
        </tr>
        <?php
        foreach ($records as $r) {  
            ?>
            <tr>
                <td><?php echo $r->first; ?></td>
                <td><?php echo $r->last; ?></td>
                <td><?php echo $r->bio; ?></td>
                <td><?php echo $r->created; ?></td>
            </tr>    
            <?php
        }
        ?>
    </table>
    <?php
}
?>

and this is the html markup

<form action="" method="post">
    First <input type="text" id="first"><br>
    last <input type="text" id="last"><br>
    Bio <input type="text" id="bio"><br>
   <button id="submit" value="click me!!"></button>

</form>
</body>
</html>

I am trying to get inputs values from index file like this below But it doesnt work this way It returns object

$(document).ready(function () {
    $('#submit').click(function () {
        var name=$('#last');
      alert(name);
    });
});

thank in advance

2
  • 2
    var name=$('#last').val() will work Commented Oct 1, 2016 at 13:54
  • Your form elements must have a name/value paring i.e. <input type='text' name='first' value=''> - the id is NOT sent along with the data. Commented Oct 1, 2016 at 14:26

3 Answers 3

1

When selecting an element with

$('#last')

you are getting the entire element in return. With that, you can use the val() method to retrieve the elements value:

var name = $('#last').val();
Sign up to request clarification or add additional context in comments.

Comments

0

This will not return the value of name field:

var name=$('#last');

You can use like:

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

Also note that, you are missing the name attribute in input fields. Without name attribute you can't get the value in $_POST.

Comments

0

You are referring to the object so you will get the object.

var name=$('#last');

If you need the value you have to ask for it:

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

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.