When the user clicks the submit button, I was to pop-up a confirm box. My problem is that when he presses OK, I want to run one query. When he presses Cancel, I want to execute a different query.
Can this be achieved with only PHP and JavaScript (Not AJAX or JQuery)
If Yes, Please tell me how. Thanks.
Edit: My main problem is that if I attach the onclick function to submit button, it is executing the server side code before taking the confirmation. And I cannot add the onsubmit function to the form since I have two action items associated with this and I want this feature for only one button. Any thoughts on this?
Create a hidden input field. Based on user's selection set it to different values using JS and then parse those values using php script to identify what should be done
Use Hidden field for user selected value like OK or Cancel.Change the query based on the user selected value.
You can call confirm()
function myfunc1(id) {
if (confirm("OK to submit?")) {
location.href = 'delete.php?id='+id;
}else{
// somthing else
}
}
Or you can call it in the onclick:
onclick="if (confirm('OK to submit?')) { myfunc1('<?php echo $uid;?>'); } "
use this way...
<input type="submit" onclick="return confirm('Are you sure?');" value="submit" >
//if user click ok then your form will submited otherwise if user click cancel it does nothing .
may be this will help
Related
I have a form with multiple inputs. I'm trying to specifically send text inputs to a function and have the function return the var as a table. I am able to send the input and return a table, but the table only appears for a brief second when I click a submit button, then the page seems to refresh. I have the submit input set to onclick="function()".
Does anybody have any ideas as to why after I click submit, the page refreshes and I loose the data in the table? I would appreciate some ideas of what the proper way is to go about this problem. Thanks in advance.
you can use onclick="function(event){event.preventDefault(); /*your code here*/}" to cancel the refresh
One option might be to use a regular button instead of a submit button:
<button onclick="function()">Function</button>
By the way, I really hope your function isn't actually called function(), but that's a whole other problem...
So you have a checkbox or a radio button with a predifined value to be sent to the database:
<input name="statement" type="radio" value="AWENSOME">
But someone or a script, with bad intention can easily change the value of your checkbox/radio button with for example a basic "browser page inspect" and then send other value to the databse. For example:
<input name="statement" type="radio" value="NOT SO AWENSOME! STUPID">
How can one prevent that guys? Thank you.
No, you can't, the best way is to gather all allowable input values in the database and check those values everytime on the server. It is easy in case of inputs like checkbox, select, radio, because you know exactly what the values can be. In case of text inputs, you have to use regex and sanitanization.
Maybe something like this in your model would help if your are using php:
if ($data['statement'] == 'AWENSOME' || $data['statement'] == 'FOOBAR' )
{
$statement = $data['statement'];
} else
{
// abort the app or return an error to the user
}
You cannot totally prevent the user from modifying the html scripts in the browser, but you can prevent unnecessary data to enter in your database..
In order to prevent that, you should have a validator in your php scripts in the server side.
There are many ways in preventing invalid data to enter in the db:
make a list of valid values in the database and once the user selects it, the server will check if the value in the checkbox or radio is existing
make the value fo radio/checkbox an encrypted or lets say there is some unique format like zkdie23doo44s that can be identified by your server..
periodically, check the html checkboxes and reload the values based from the original html script in the server
hope this helped you get an idea or two..
You can't do that you have to check once again on the server side and for the boxes like check box you know the value and for text box you can use regular expression
I have a form (HTML) with some inputs (checkbox) and I want to submit only inputs that user has clicked ou unclicked and ignore the others inputs.
I'm thinking in add a class (javascript) to the input when user change value. But I don't know if it is a good ideia.
Any Suggestions?
You can have the form that submits onClick() and calls a Javascript. When that happens you then search what values have been unchecked and then use the Javascript to submit it to the next page to handle the response.
However not sure what you are trying to achieve doing this...
Class approach is working.
When user click on input I add class "envia":
$("INPUT[type='checkbox']").click(function(){
$(this).addClass("envia");
});
On submit I send only data with the class I want:
$.post("/financeiro/funcional/contasapagar.php", $(".envia").serialize(), function(data){
//code here
});
Only data the user changed is send to server.
I'm new to this kind of stuff so sorry if this is not possible or does not make any sense. Im making a app where someone puts in there username into the form and when they press enter a popup says words but then i want for after they press ok it puts the username into the form ---- instagram://user?username --- where it says username thats where i want the username from the form to go then it will go there. sorry if this doesnt make sense or is not possible wondering if it is
You want to set your form action to GET. I believe that is what you are trying to ask.
<form action='[url submitting to]' method='GET'></form>
Using jQuery, if this is the language you are working with, you could prevent the submit and get the username field and add it to url and load this page.
//html
<form>
<input id="username">
</form>
//javascript
//the submit event is captured
$('form').on('submit', function(e){
//the event is passed in on the function and is used to prevent the
//default action so we can perform some further action
e.preventDefault();
//here we grab the value of the the #username text field
var user = $('input#username').val();
//here we are loading the instagram page based upon the user's textfield
window.location = 'instagram://user?' + user;
});
Is this what you are looking for?
I have input field
<input type="text" name="vehicle_make[]" id="make1"/>
and i have help dropdown that updates this field if user choose to do so. I do it trough standard jquery code
$("#make1").val("value");
Problem is, since i use validate plugin to validate this field, if user click on validate before entering anything in that box, he will get notice that he needs to fill it, but then if he fills it trough dropdown, validate plugin will not detect it until user click on submit again.
I know i could call submit in function in which i process dropdown, but i think that it is not right solution, since i would need to check if validation is already done before doing that (since if it is not, it would start validation before i want it to start).
I also need to tie some other things to that field also, so i would like to know is there is a way in which i could write function so it always check if field is filled, even if user did not fill it directly.
Sorry if i didn't explain everything right, this is my first message here.
Try this
$("#make1").change(function(){
//do something there
});
I have found solution. First, i created js variable form_submitted, and added onclick event to submit button, to change value of variable form_submitted to yes. Then, i created function:
function update_validation(){
if(form_submitted == 'yes'){
$("#my_form").valid();
};
};
that i call when user do something that is not detected regularly with validate plugin. This function manually starts validation again, but only if it has been started before by clicking on submit.