Insert a date into the database via jquery+php

Asked

Viewed 66 times

0

I have a datepicker like this

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />

    <form action="dashboard.php" name="form1" class="form1" method="POST">
        <input type="text" id="dtSelectorStatic" />

    </form>
</li>

And a script like that:

jQuery(function($) {
    $("#dtSelectorStatic").datepicker({dateFormat: "dd-mm-yy"});
    var d = new Date();
    var month = d.getMonth()+1;
    var day = d.getDate();
    var output = d.getFullYear() + '/' +
    (month<10 ? '0' : '') + month + '/' +
    (day<10 ? '0' : '') + day;

    $("#dtSelectorStatic").datepicker("setDate", new Date(output));

    console.log(output);

    $('#dtSelectorStatic').on('change', function() {
        console.log('Submiting form');
        console.log(output);
        $('#form1').submit();

    }).trigger('change');
});

I have a function to insert all data into the database and I need to pass this datepicker value to the insert function

Note: The whole code is on the same page . php

  • I don’t understand what you want

  • inputs need the tag name to be identified by the PHP script

  • I want to use the variable "output" that contains the date chosen by the user for later on pressing the insert button, it insert in the database all the information plus the date, but I can’t use this variable "output" in php of the same page

2 answers

0

I still don’t understand why not use one:

<input type="date" name="data" id="dtSelectorStatic">

<script>
$("#dtSelectorStatic").change(function(){
$("#form1").submit()
});
</script>

If what you want is to submit a form with the change of a date there is your answer, if not explain better.

  • If your error is in PHP or DML add the information in your question!

  • The problem is to use the "output" variable of the function for another php function that I have on the same page to insert into the database

  • whether you are on the same page or not, if you want to send a data from a form to a PHP function you have to submit the form to the php page save this data in a variable and send this data as parameter to the function!

  • tell more openly how the program should work maybe what you want to do this way has a simpler solution.

-1

Have you added the part that makes the request? What’s this I’m not seeing.

Executing your code. I noticed that the date comes in "Brazilian" format. Which is the format that represents, respectively: dd/mm/YYYY.

Will the field that will receive the date, in the database, is with that same pattern (dd/mm/YYYY), or it is in the pattern "YYYY-mm-dd"?

Depending on how this value will be stored in the database, you have to convert to such.

Check the description of the fields of the table which data format is accepted. Then just convert to it (if it is of type Date, in the YYYY-mm-dd pattern).

Hugs!

  • Yes and in Portugal we use this method, day/month/year and I want the user to use this same way, in the function of adding the record I convert the date with the following code: $_SESSION['data'] = date('Y-m-d',strtotime($_SESSION['data'])); But I have to fetch the date from the above function to use in the insert function

  • Well, there are still some pieces missing. Try placing here all the pieces that make up the flow of the date registration. I say this because there must be some detail that is in your insert file and the database table. Do the following: see if the value you are trying to register is really coming to the insert script page. Do it calmly and slowly. If possible, note on paper the flow of what this record insertion should look like. In the middle of the way you will find the solution. Or put in all the code fragments so you can analyze them better. Hugs!

  • I think there is no error, I just wanted to know if there is any way to pass the variable "output" of the function because it gets the value of the date I want for that function in php that makes the insertion. I don’t know how to send a jquery variable to php

  • I accidentally commented here, I’ll comment further below...

  • If you want to do it on the same page. Forget it. Otherwise, the value of your output variable will be available in the body of any of these requests: GET, POST, PUT or DELETE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.