I have the variables in the current file initialized, either by POST
or GET
, for example:
if (isset($_GET['email']) && preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $_GET['email'])) {
$email = $_GET['email'];
}
if (isset($email)) {
SELECT * FROM usuarios WHERE email=?
//...
}
Now through logged user sessions
if (isset($_SESSION["id_username"])) {
if (!filter_var($_SESSION["id_username"] ?: '', FILTER_VALIDATE_INT) === false) {
$id_username = $_SESSION["id_username"] ?: '';
}
}
Now for user data to be printed globally
$Username->bind_result($email,$nombres,$ciudad //.... );
I have all this data, in my applications I use ajax
, and I only want to be able to add fields input
that are necessary and those that are not necessary to be able to send that information directly from the code ajax
without the need to create input
hidden fields, since each formulario
one has an id
identification , which ajax
only works for that form var frm = $('#resetform');
.
So how can I send that information without the need to create an input field? for instance<input type="hidden" name="email" value="<?php echo $email;?>">
this value $email;
can be sent directly with the ajax code, some parameter, I invent var $email
and thus send that information.
$(function() {
var frm = $('#resetform');
frm.submit(function(e){
e.preventDefault();
var formData = frm.serialize();
formData += '&' + $('#submit_btn').attr('name') + '=' + $('#submit_btn').attr('value');
var url = "update.php";
$.ajax({
type: frm.attr('method'),
url: url,
data: formData,
})
.done(function(data) {
let res = JSON.parse(data);
if(res.status){
$('#message').fadeIn();
$('#message').html(res.message).delay(3000).fadeOut(3000);
$(frm).hide();
setTimeout(function(){
location.href = 'index.php';
},2000);
} else {
$('#message').fadeIn();
$('#message').html(res.message).delay(3000).fadeOut(3000);
}
})
.fail(function() {
$('#message').fadeIn();
$('#message').html(textStatus).delay(2000).fadeOut(2000);
})
});
});
edited
I think you could use new FormData() , where you can store all the data of the fields of your form and also add new values without having to add hidden fields, I will refer to the part of your code where you need it. Example:
If you pay attention to the formData variable, it would store all your form #resetform and after initializing it you can add separate values like doing a push to an array with append , I hope this answer will help you.
Another way with $_COOKIE
As I told you, another way to do it without having to mix php with js is to create cookies from php and read it from js:
PHP
The sessions use a cookie as a kind of key, to associate the data that is stored on the server side. On the server, when you create a session or a cookie, a file is created on that server and when you need to read a value from that cookie or session, it reads it from the file, sessions and cookies use the same file to store values within the server for a web visitor. That said, you can safely use cookies to store values as if they were sessions. Now the idea later would be to read the values in the js like this :
Example when you need to retrieve the values and send them by ajax
We only create an empty form, without any fields
Then we capture the submit event, and add the Field we need with the necessary value
If I managed to understand your question well, then my solution would be the following. You do not want to have extra input fields in your form, so perfect, mix the data you want to send with ajax jquery. I really don't know what sense it makes to forward data to the server that the user doesn't modify, but hey, it's your solution and you'll know what you're doing. I would do it in several ways, this is one:
code:
I suggest you create a general shipping code, something like this:
GENERIC FUNCTION TO SEND VIA AJAX:
Now imagine this is your form field:
You would only have to create the following click event for all the forms that you want to send more extra content with AJAX:
It is always good to apply design patterns and generalize your code. Hope this can help you. Cheers!
It is not necessary to capture the data to send it by AJAX, from what I see it already exists in $_SESSION, therefore you have said data on the server side, it is not necessary, from what you say you could do to send only the specific data the AJAX request like this:
$.ajax({ type: frm.attr('method'), url: url, data: { key:value, key2:value2 }, // ajax code here... });
Well, several years have passed since this publication but I am sure that, like me, someone else will have this doubt in the future.
If you are sure that your session variables that you have created are well propagated throughout your system, then the solution is just to update the session_start, which for some reason when a php script is accessed via Ajax the session variables that you I think they are there but they are not accessible or the request does not find them, all it would have to do is something like this: