Main Problem :- Ajax: How to pass both form data and credentials on submit?
I have the below function which is called when a user clicks on a submit button:
<script type="text/javascript">
function submitForm() {
console.log("submit event");
var fd = new FormData(document.getElementById("campaign_form"));
console.log(fd);
var username = $('#username').val();
console.log(username);
var password = $('#password').val();
console.log(password);
$.ajax({
type: "POST",
url: "http://localhost/testing/post_campaigns.php",
data: fd,
data: {
"username": $('#username').val(),
"password": $('#password').val()
},
});
return false;
}
</script>
My php URL is called properly, but I don't see that my data is passed.
For example, I would typically expect to see in developer tools:
http://localhost/testing/post_campaigns.php?username=XXX&password=YYY
But I only see:
http://localhost/testing/post_campaigns.php
My variables are written to the console properly so I know they exist and have values.
I'm sure the issue is along the lines of my syntax; but I'm not sure how to format my Ajax properly to accommodate both object FormData and string variables username/password.
Basically, my overall code asks the user for a sheet they have filled out with specific values under designated column headers, and I read that information and POST it to our ad server. So I need both the FormData and the Username/Password provided by the user to be passed to my PHP file when I execute the POST request to our ad server with the information/changes requested by the user.
If my question/dilemma is not clear or you need more code, please let me know.
Thank you,
Edit: Attaching a screenshot of FormData in console per request.
jQuery / AJAX Code:- For Sending formdata with other javascript variable together.
<script>
$('#btn').on("click", function () {
var formData = new FormData($("#form1")[0]);
formData.append('ipid',id); //id is the variable that has the data that I need
var path = "http://localhost/testing/post_campaigns.php",
$.ajax({
url: path,
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function (stuff) {
$("#resp").html(stuff);
}
});
});
});
</script>
Note:-
To append param just useappend()
method:
formData.append("param", "value");
And in the php-side I catch it:
echo $pid = ($_POST['ipid']);
Note:- For more reference regarding this check this
https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.