How To Read Post Url Request From Javascript In Php
Solution 1:
Ok I will explain with example,
If you want to get url parameter values to you have to use,
<script>let my_variable='<?phpecho$_GET['url_param_name'];?>';
</script>
above for more help and understanding. Now you want to send form data to php for processing as I got your answer.
This is sample form.
<formid="my_form"name"my_form" method="POST"onsubmit="return send();">
First name:<br><inputtype="text"name="first_name"value="Mickey"><br>
Last name:<br><inputtype="text"name="last_name"value="Mouse"><br><br><inputtype="submit"value="Submit"></form>
To post above form I will use javascript function,
<script>
functionsend() {
$.ajax
({
type: 'POST',
url: './path/your_php_file_where_form_data_processed.php',
data:$('#my_form').serialize(),
success: function() {
// do what you need to do on succeess
},
error: function(x, e) {
// forerror handling
if (x.status == 0) {
console.log('You are offline!! - Please Check Your Network.');
} elseif (x.status == 404) {
console.log('Requested URL not found.');
} elseif (x.status == 500) {
console.log('Internal Server Error.');
} elseif (e == 'parsererror') {
console.log('Error. - Parsing JSON Request failed.');
} elseif (e == 'timeout') {
console.log('Request Time out.');
} else {
console.log('Unknown Error. - ' + x.responseText);
}
}
});
returnfalse;
}
</<script>
Now you need to check carefully your form element names. In php file. Let's look it.
<?php//include_once './../../classes/Database.php'; import if you have database configurations//session_start(); make sure to use sessions if your site using sessionsif(isset($_POST))
{
var_dump($_POST); //this will echo your form inputed data.//if you want use one by one posted dataecho$_POST['first_name'];
echo$_POST['last_name'];
}
else
{
echo'Data not comes here';
}
?>
Thought this might help your task.
Solution 2:
When you see the form elements as a query string, you're not making a POST request, but a GET request.
Change your Ajax code to:
$.ajax({
url: path,
type: "POST",
data: formData
});
What's changed:
- I've changed
method: 'POST'
to the correct attribute:type: 'POST'
, which will make the Ajax request to make a POST request instead of the default GET. - Changed
data: {formData: formData}
to justdata: formData
. There's no need to post it as json when you've already serialized the data.
If you rather want to use $.post
, then use:
$.post(path, formData);
Just to be clear, both the $.ajax
and $.post
-requests above will make identical requests.
Now, in your PHP-code, you can access the values like this:
$calcOwnership = $_POST['calc-ownership'];
...to get the posted data.
I would recommend that you read the documentation about the methods you're using:
Solution 3:
I may have misunderstood your question, but in php you can take values from post using $_POST['variable_name'].
<?php$documents_count = $_POST['documents_count']
?>
In fact this, in your url is not a query, are variables. sorry if it's not what you asked.
Post a Comment for "How To Read Post Url Request From Javascript In Php"