0

I make a form with 2 input, one is text another is hidden that store the value of my variables. Then I write a jquery script with post method to pass the value to another php file which will insert the values to a database, But only the value from text input are inserted, the value from hidden input doesn't work. Here are the form and the jquery script:

<?php
include_once('header.php');
$uid = $_GET['uid'];
echo $uid;
[enter image description here][1]?>
 <form>
        <input name="usermsg" type="text" id="usermsg" size="63" />
        <input type="hidden" name="uid" value="<?php echo $uid; ?>">
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
    </form>
 $(document).ready(function () {
        $("#submitmsg").click(function () {
            var clientmsg = $("#usermsg").val();
            var clientid = $("#uid").val();
            $.post("includes/postchat.php", {text: clientmsg,uid: clientid});
            $("#usermsg").attr("value", "");
            return false;
        });

And here is the postchat.php file:

if (isset($_SESSION['u_uid'])) {
include('dbh-inc.php');
$text = mysqli_real_escape_string($conn, $_POST['text']);
$uid = $_POST['uid'];
$date_current = date("Y-m-d H:i:s");
$user = $_SESSION['u_uid'];
if ($user = 'one') {

    if ($text != '') {
        $sql = "INSERT INTO messages (body, user_from, date_sent, user_to) VALUES (

            '$text',
            '$user',
            '$date_current',
            '$uid'    
        )";
        mysqli_query($conn, $sql);
    }

My table in database after submit:

1
  • 1
    There is no element with the ID of "uid", as referenced by var clientid = $("#uid").val(); Commented Jun 15, 2018 at 20:25

4 Answers 4

1

change this

<input type="hidden" name="uid" value="<?php echo $uid; ?>">

into this

<input type="hidden" id="uid" name="uid" value="<?php echo $uid; ?>">

As you have taken only name as uid but try to get the value of #uid which is not present

Sign up to request clarification or add additional context in comments.

Comments

0

Put the id field in the hidden element as uid.

<input type="hidden" id="uid" name="uid" value="<?php echo $uid; ?>">

In JQuery if you are accessing elements using '#' it means that you are accessing them by their ids.

Hope this helps.

Comments

0

As the answers stated before, there is no element with id uid. You should either change your element tag to

<input type="hidden" id="uid" name="uid" value="<?php echo $uid; ?>">

Or use the following jQuery code:

var clientid = $("input[name='uid']").val();

Comments

0

You can just do this:

 <form>
        <input name="usermsg" type="text" size="63" />
        <input type="hidden" name="uid" value="<?php echo $uid; ?>">
        <input name="submitmsg" type="submit"  value="Send" />
 </form>
<script>
$(function () {
    $("form").submit(function (e) {
        e.preventDefault();
        $.post("includes/postchat.php", $(this).serialize());
        $("#usermsg").attr("value", "");
        return false;
    });
});
</script>

note

$(this).serialize() will serialize all form fields, so you can easily add more fields without adding them one by one and you don't need to set any id.

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.