I have a script that I downloaded and want to add an href="#" to it but it's interfering with the form on the page. The form is triggered by a button, not sure of that matters. I tested my href outside the form but it still refreshes the page once before it works. Here is the code.
<form method="POST" action="/profile/save_profile_setting">
Update My Location
</form>
<button class="btn" type="submit" name="action" ><span><?php echo __( 'Save' );?></span></button>
My href just passes the id to run ajax to find a location, it's always worked but now the two aren't playing well together. I have the same href on a different page by itself and it works fine.
Thanks for any help.
I didn't understand what is your actual query. I understood roughly of your query. My understanding for your question is that: with tag, you want to execute ajax call and with button, you want to perform form action. Based on this assumption:
(1) If you want to avoid page refresh with tag, you need to do as follows:
Update My Location
Now, you can easily perform ajax with tag.
(2) To perform button action with form, you need to add button inside form instead of outside the form.
Related
I'm building a website in which the user can create articles with multiple images.
I would like to let the users while editing the form to can delete some image that they want. So I want to have in my form the main submit button that execute the function that store all details of article but also to I have another submit button that execute the function that delete the image.
How can I do that?
Thank you so much for your attention and participation.
If I got you right, you want 1 FORM, 2 SUBMIT buttons, and based on which one you press, do a different action? It is possible to do this, but not very practical.
To do it, create 2 submit buttons with each a value. I.e.:
<input type="submit" name="mysubmit" value="delete image" />
<input type="submit" name="mysubmit" value="send the form" />
when doing this, your post/get data will contain one item names mysubmit with the value, so you know which button was pressed, and you can do an action based on this.
However, when you submit a form by pressing a submit button, you do send the whole thing to your server, and have a page refresh. I usually prefer to use Ajax for the simple operation. For example, I would remove the delete submit button and replace it a simple button. When pressed, send an Ajax call to tell the server to delete the image, and use DOM to delete the image in the browser DOM tree (usually jQuery). Note that you can also use Ajax to post the form, nicer interface, and no page refresh.
I'm making a php site that has multiple buttons. One button saves, which requires it to be taken to a different page but the other buttons do simple things like add +1 to a score.
The function buttons are attached with javascript so the code looks like this for a function button:
<button onclick="addPoints(); ButtonStats()">Add</button></td>
I have tried multiple different ways to get the buttons working but I'm not sure how to do an if statement like this:
if ($_POST['SaveButton'])
$executestring = "location: process.php";
The if statement works for the save button (but clicking the other buttons currently refreshes the page).
So, how would I go about writing in the add button codes to an 'if' statement like that of the submit button? (I've tried putting in addPoints() for the executestring but that didn't work so I'm just not sure how to write it out).
Don't use <button> without the type attribute defined as button, cause they will try to submit the form, causing the page to be "refreshed". Use
<input type="button" value="click" />
or
<button type="button">Click</button>
if you don't want them to have the submit behavior.
Look at this jsFiddle example
You are calling javascript when button Add is clicked which means you can not do anything about it in php.
When you call javascript, all calculation is done on client side (for example users browser). Php is executed only when users comes to your page (because page is requested from server, server executes php and sends result).
It suggest you learn more about client side and server side languages.
On my current project, the user clicks on "Save Changes" on what they are editing, and it is further down the page where the edit box, saved text, etc... pops up. Is there a way I can have the site retain its page location in order to auto-scroll them back down to where they clicked edit?
Ideally I'd have some type of solution where the page wouldn't have to reload but I don't know how to do that, lol.
My site is coded in PHP.
Thanks
There is one functionality in Html to position your page with the help of using (#).
For example considering the following scenario where your Edit button resides
<div id="editButton">
<input type="button" name="Edit" value ="Edit"/>
</div>
If your page name is "index.php" and you redirect with url : "index.php#editButton"
Your page will automatically scroll to that section without much efforts.
It identifies the id of the element and put the scroll up to that position.
Cheers
You might want to have a look at some tutorials on how to save a form via AJAX. This will mean you aren't POSTing the page, and therefore it won't refresh and the user won't lose their position on the page.
http://www.jstiles.com/Blog/How-To-Submit-a-Form-with-jQuery-and-AJAX
http://www.devblog.co/easy-jquery-ajax-php-contact-form/
I have simply put name of ID selector in form action:
<form class="form-horizontal" method="post" action="#regform">
And it works for me.
Yes, it is good to have that approach.
Rather than complete redirection, only a chunk of data should be sent over and uploaded accordingly.
For this, you need http://www.w3schools.com/ajax/default.ASP
then, learn jQuery (hope you familiar with what is an id and class in CSS)...
http://api.jquery.com/jQuery.ajax/
Cool?
If you have multiple forms on one page, you could add name attribute of that form to link. e.g.
<form name="my-form1" action="form.php#my-form1">
I guess this should work.
Another way is to consider using Ajax, so you dont have to reload page at all, or you can create javasscript function that will be called on form submit and will add current page position in hidden input. After page reload, you can scroll to original position using value from that hidden input.
Yes, use Ajax to update the page partially. This is the way to do it in any web technology.
I wanted some input as to what the best way to handle this would be. I have a submit button and a normal button inside a form. I don't want to do a submit on my delete button for the form. Is setting a link outside of the button to carry over into another file using a $_GET parameter the best way here? Basically, take the GET parameter in the php file and if its true, then do my delete functionality. Is there a better way here?
e.g. <input type="button" value="Delete Item" />
GET requests should be used when accessing data (SELECT). POST requests should be used when modifying data (UPDATE, CREATE, DELETE). i.e. You shouldn't be using a GET request to delete a system resource.
I had a similar thing, and there where two methods I used:
The first was that I used a standard button which had the onclick attribute set to a javascript function that would change the value of a hidden input and would then submit the form.
The second was a submit button, but following this question: Position div box at the end of after ensuing elements, I had the main form submit (that would save) at the beginning of the form, which then appeared at the bottom of the form. This mean't that when if the enter button was pressed, the delete submit wouldn't be clicked (and detected by the script), but the main submit would be.
I'm doing a questionnaire (form) and I need to put a submit button that does two things:
Be a button type INPUT (because I need to use this kind of button on my PHP code, I've if(#$_POST['Next']) for save the dates of the form in my DB).
That this button will have a link for go to the next screen of the questionnaire. I tried with
<a href="demo2.html" target="_blank">
<input class="buttonNext" name="submit" type="submit" value="NEXT ←">
</a>
This code doesn't work, but with IE browser, on the page appears a circle next to my button that is the link. So the button doesn't work; it only saves the data, but doesn't link to the next page.
How can I solve it?
The form's action determines the URL where the form is sent, and this is just good for you (it is sent to the PHP that processes this step). It can save the data and/or return the form again to show validation errors. Once you decide to go to the next step, you can redirect the user to that url.
use if(isset($_POST['Next'])){ instead of #