Sending POST data via href - php

Is there any way of sending post data via an anchor tag??
for GET data you just have to write it directly in the URL
but can we still do it for POST?
i need the link to be without any parameters and pass name=john via POST. can this be done?

Not with plain HTML.
You could use JavaScript to cancel the default behaviour of the link and submit a form (creating the form if needed).
jQuery('a').on('click', function (evt) {
jQuery('form').submit();
evt.preventDefault();
});
… but you shouldn't do that because links and submit buttons have different affordances and you'd be sending misleading messages to the user while adding an unnecessary dependancy on JavaScript.
If you want to make a POST request, then do it for the right reasons and do it with a form and a submit button.

Related

Form with only a button

I have a html project with PHP and I want to be able to have a button that when I click on him, a POST request to be executed. The problem is that I want to be able to also send some parameters through the request but I don't want to have input fields.
I am afraid to say that it is impossible when you only use HTML each element of HTML have their usage and you can not send parameters like what you described the only thing you can send by a button is its name and value solutions to this situation is using hidden input type or sending these parameters by AJAX or socket as JSON for example. any sub-element of form tag in HTML will be received to the server as post global array and they generate by input fields inside the form so you can not have another input in the server if you don't create it in HTML unless you don't use HTML for requesting to the server I mean it's possible but not with HTML

prevent dupplicate submitted by ajax

I'm using ajax to create list of replies.
In the end of the list I added textarea form that allow user to add reply (also with ajax).
The problem is that i call to my JS in my main PHP page so when user want to submit reply the page doesn't "know" the js code. If i add the js to the ajax php file/page the code will work but then it will be duplicated many times and when user will submit form the text will be submitted many times...
In my console i see that the JS file duplicate every time i load the replies list
How can i prevent it?
Disable the submit button right after user presses it once. Using jQuery:
$("#button").attr('disabled','disabled');
Make sure to remove disabled attribute on AJAX error so user can re-submit the form with new data. You can remove disabled attribute like this:
$('#button').removeAttr('disabled');
[improved]
if you want to not repeat data when navigation or F5 press, simply free the $_POST vars after doing whatever you want, and check if isnt set (before clean, of course) redirect you wherever you want. example:
/* HERE do the job with ajax response. */
if(!isset($_post['foo'])) header('Location: wherever.php');
$_POST['foo']=NULL;
If you're using $_GET... don't do it, use $_POST... $_POST is your friend.
Finaly ensure that if you press F5, you don't re-send form vars by post (otherwise you will get the same). If it's happening, clear your inputs with jQuery on page load.
$('#inputID').val('');

How to submit part of inputs from a form to another domain?

When user create a post, inside the post form, I want to provide a button. When user click the button, it will launch an iframe and submit some of the inputs of the post form. When nested form is not valid, how can I submit those inputs to the other website?
Two methods, Javascript, and PHP
Javascript:
You can use jQuery Post to do this by javascript.
Basic steps:
Use an "onsubmit" handler for the form
In the function for handling the submit, build a data array with data you want to submit and send to the second form.
Either return true (to allow the programmed submit to continue) or build a secondary submit if required.
This one allows you to build the validation you refer to client-side (i.e. in the submit handler).
PHP
Receive the data in the php form, then use CURL in the receiving PHP script to pass data as needed to the second form.

submitting JavaScript click through curl and PHP

I am trying to scrap a web, On the main page, there are a few option buttons and then an ACCEPT button, and RESET button, when ACCEPT button is pressed after selecting appropriate option, new contents are displayed,
my question is, how can I sent a request through curl, when i inspect the ACCEPT button a JavaScript function is initiated, like
JavaScript:someFunction();
Can somebody guide me what should I do? how can I do it via curl and php? Or I should learn some new technique for this, my guess is, the ACCEPT button is sending an AJAX CALL,
guide me please
You need to call that ajax url using cURL and also set those desired variables along with proper values which are set after clicking that button. You can check cURL form posts.
Thanks.
Are you trying to simply click the button and submit the form? If that's the case, you can do that with plain javascript:
document.getElementById('acceptButtonId').click();
When the button clicked, you can post the data into the same page. Then with the variables you posted, you use cURL.

How to send set of data for Ajax call from jquery with php at the serverside?

I basically have to do a update of a record. I have a set of controls like textbox, list box, radio button etc. Then i have a button on click of which i need to carry all the updated data into mysql database with a ajax request without page refresh.
I am using the php with codeigniter as my serverside code. On client side i am able to send the ajax request like
$(document).ready(function(){
$('#users_menu').click(
function(){
$('#tempdiv').load('http://localhost//web1/index.php/c1',null);
}
);
});
In the above code the request is placed to a server side php page where i am not able to read the values of the control values (values of textbox, listbox etc). Now this means i should be sending the list with the request. But i am not aware of how to send this request.
Please help me with some details of how to send the list of values or is it possible to read the vaules some how in the serverside php code. For your information i am using codeigniter with my php. Any kind of help is appreciated.
Thanks and Regards
VinodT.
You need to use jQuery .post() function and specify the data to send, see here jQuery.post
You will end up needing to do something similiar to this.
$.post("http://localhost//web1/index.php/c1", $("#testform").serialize());

Categories