How do I submit my form using href? - php

Am using this onclick='document.forms['form_name'].submit(); return false;' but this doesn't work as am having href=results.php?page_no=1 etc, has dynamic links, examples show to make this work I need to use href="#" but any idea how I can submit the form and than navigate to page2? Because I want to save the check box values in a session

Add class to your hrefs (class="pagination") and id (id="form") to your form. Then you can use Jquery framework for this stuff.
$(".pagination").click(function(){
// get page id, set form action with params
$("#formId").submit();
return false;
});

You have a bad design.
You can't perform multiple competing actions on a form click and expect it to work.
You need to either let the link be clicked and let it load another page, or if you are just setting some session variable (although it would be far better to set this with a querystring parameter or a cookie), you can use an Ajax request to send that off asynchronously.

Here I substituted page2 with Google just for test
Submit
<form method="get" action="https://www.google.com/search?q=test" name="test">
<input name="Checkbox1" type="checkbox" />
</form>
edit:
Submit
<form method="get" action="" name="test">
<input name="Checkbox1" type="checkbox" />
</form>
without encodeURIComponent(this.getAttribute('href') the parameters are missed.

Some options:
1) Use jQuery AJAX, serialize and post the form data and then redirect (location.href) on the onSuccess callback. Something like this:
$.post("submitform.php",
$("form").serialize(),
function(data){location.href='results.php?page_no=2';}
);
2) Post the form to a named hidden iFrame using "target" on the form tag. If this is really just a best effort sort of recording you shouldn't need to wait for the page to load, the request should be enough and you can continue to the next page. Something like this:
<iframe="targetname" style="display:none;" />
<form name="myform" target="targetname" method="post" action="submitform.php">
....
</form>
<a href="page2.php" onClick="document.forms['myform'].submit(); return true;">
Click Here
</a>

Related

How to not refresh page's previous field input values after press submit

I have an input like this:
<input value="<?php echo $formdata['title'] ?>" type="text" name="title" id="Editbox2">
This is an edit page, I load database data into fields with echo, replace them, and hit submit to update them.
But when I hit submit it refreshes the old data onto browser's fields, how can I prevent this?
Submit your form using ajax request with jquery submit.
Use action="javascript:;" for the form tag
You need to handle the script with javascript, then prevent the default behaviour, which is refreshing the page. Here is an example:
*I haven't tested this, but from what I recall this is what I used to do. Let me know if it doesn't work, I'll give other suggestions.
<form>
<!-- elements inside -->
<input type="submit" id="submit-btn" value="Submit"/>
</form>
and in your javascript have the following:
<script>
$("#submit-btn").click(function(e){
e.preventDefault();
// handle form here with your JS
});
</script>

Submit button showing in GET URL

I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>

php post via html <a> tags

I have built a site using php and want to try keep it one page.. The site displays pictures and so far i have it making links from folders in a folder each folder contains images so what i want is to make a post/get tag in the url and when the page loads it uses this to get the images from that folder.
So I want to use the generated links to post to the same page with a value via self_post is this possible and if so how?
my get section is
if(empty($_post['foldername']))
{
$directory = "portfolio/homepage/";
}
else if(isset($_post['foldername']))
{
$foldername = $_post['foldername'];
$directory = "portfolio/".$foldername."/";
}
and my link is like this
echo '<li><a id="" class="" href="'.$_SERVER["PHP_SELF"].'">'.$b.'<input type="hidden" name="foldername" value="'.$b.'" /></a></li>';
Thanks
What's wrong with GET?
Click me
The only way to make a POST request using a <a> tag would be to have it submit a form via javascript:
<form method="post" id="hidden_form" name="hidden_form" action="script.php">
<input type="hidden" name="foldername" value="<?php echo $b ?>" />
</form>
...
post me
You can also update the values of the hidden element(s) from javascript as well so when you click a particular link, it sets one of the values to something specific to the link.
The only way is doing it through JS. You can either send an AJAX request specifying POST, or you can create a hidden form and submit it. Here's an example
document.getElementById('my-link').onclick = function(){
// Code to submit the hidden form or to send an AJAX request specifying POST
return false; // to prevent the default behavior
}
I know of no way to do this with vanilla anchor tags. You could establish click event handlers in javascript to submit an XHR request. However, I have accomplished this in the past by using multiple form tags with a single submit entity (<input type='submit', <button type='submit', etc.).
Your forms would look like so:
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="hidden" name="foldername" value="YOURVALUEHERE">
<input type="submit">
</form>
Like drew010 said if you absolutly need the POST method. Otherwise most single-page website uses things like index.php?a=1&b=2 where you can get "a" and "b" with $_GET["a"] ...

PHP code regarding multiple submit button on single form

i have two submit button on my index page namely International and Domestic. i want that two different button to point to different pages namely int.php and dom.php when i click on the buttons. can you help me out. thank
while it is allowed only to define single action = "" for form element. but if i have to do that, i would do it this way.
<form action ="somepage.php" method="post">
<!--all your input elements-->
<input type="submit" name ="international" value="international"/>
<input type="submit" name ="domestic" value="domestic"/>
</form>
determine which button have been clicked and act accordingly.
if(isset($_POST['domestic']) {
//include dom.php
}
else if(isset($_POST['international']) {
//include int.php
}
and then you can include the necessary file.
or the other way is to go with AJAX/jQuery way
you can just use switch in php for differ or
use javascript
Do it with jquery! First, dont create submit buttons just create
<input type="button" />
Than give them an id like:
<input type="button" id="InternationalBTN" />
<input type="button" id="DomesticBTN" />
and with jquery bind the action
$(document).ready(function(){
$("#InternationalBTN").bind('click',function(){
$('#idOfYourForm').attr('action','setTheDestinationPageHere');
document.forms['nameOfYourForm'].submit();
});
});
That will not be possible since your form's action attribute can only point to one location at a time and both buttons are in the same form(but possible if you use AJAX).
If you wanted to use pure PHP (i.e. no Javascript involved), you'd have to write two different handlers for the different button clicks, like below:
if(isset($_POST["name_of_international_button"])){
//actions to perform for this --
}
if(isset($_POST["name_of_domestic_button"])){
//action to perform for this
}
In the actions part of each of the handlers, you could then do a redirect, with the URL containing the data to be processed either in the int.php or dom.php scripts
You can do it in this way:
In form tag please leave empty action action=""
2 buttons to send:
<input class="btnSend" type="submit" name="International" value="International" id="International"/>
<input class="btnSend" type="submit" name="Domestic" value="Domestic" id="Domestic"/>
and use ajax:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
$('#International ').click(function() {
// send to file 1 using ajax
});
$('#Domestic').click(function() {
// send to file 2 using ajax
});
});
</script>
Here is how to send data using ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Your form action would have to contain some sort of conditional statement to redirect users based on which submit button is clicked
<form action="<?php if(isset($_POST['international'])){echo 'international.php';}else{echo 'domestic.php';}?>" method="post">
<input type="text" name="field1" />
<input type="text" name="field2"/>
<input type="submit" value="international "name="international"/>
<input type="submit" value="domestic "name="domestic"/>
</form>
Or you could set up your conditionals on a page specified by the form actionand have them redirect based on which button was clicked,
Just put a form tag, and set the action to the page. Then the submit button will navigate to that page where the action tag is pointing to...
Easy as that :D

Can php a href post not use a link like index.php?id=value

I want to post some value through a href. However, I do not like to use a link like index.php?id=value. Is there any other method?
I want to post test1 and test2.
<div id="div1">
<li>test1</li>
<li>test2</li>
</div>
<div id="div2">this is a <? $_POST["name"]; ?>, just a test.</div>
Use a form, with method=POST, and a hidden form field for the value you want to post. The 'href' either becomes a submit button, or have an onclick action on the link that just submits the form.
You can apparently use PHP for this, and the information is supposed to be here, but it is currently not working:
http://www.zend.com/zend/spotlight/mimocsumissions.php
Otherwise, you should probably just do this:
<form id="myForm" method="post" action="index.php">
<input type="hidden" name="id" value="value">
<input type="hidden" name="somethingElse" value="test">
<!-- more stuff you want to post -->
</form>
Post!
The form is invisible, but you need one form per link like this (or I guess you could use only 1 form total and call another javascript function from the onclick event that modifies the values of the form fields). I don't really see the benefit though.
You could use either jQuery/ajax to post your data to a backend or post the variables as said in the answer above.
You can use mod_rewrite (if you are using Apache) to modify the look of url. e.g. instead of www.test.com/index.php?id=value you can use www.test.com/value or www.test.com/index/value

Categories