I have 2 checkboxes in a form and onclick of these, some php code needs to be executed and based on the result of the code, the checkbox is checked or unchecked.
So i have written onclick = document.formName.submit(); Now it is triggering the same page and i am able to write the code. I am not able to differentiate which checkbox is checked.
I don't want to use the procedure of:- calling javascript and then storing the value of the checkbox in a variable and making this variable as invisible.
I would like to write something like document.formName.submit('checkbox1'). So that i should be able to handle the value of this or i dont know.
Please suggest me an alternative method or better approach.
What exactly are you doing in the PHP code? Is it validating whether or not they are able to check that box? Sounds like you're making an extra unnecessary step here... If you could describe your scenario a little better I'm sure someone could point you to a more efficient way to accomplish this.
You can't do it correctly using either saving values in database or passing it as parameter.
you have to store somewhere so that when page is get refreshed it must come to know which checkbox is checked earlier.
Related
I use php to manage html and now I have problem with input date in mysql.
All with my input in MySQL or update or delete in MySQL is ok but how I can make security for input data in mysql because if some one open to see my html source code with browser he can see my predefined inputs and he can change thats in html and after that enter wronk inputs in mysql.
This is my code:
Options Value: <select name="extend">
<option value="<?php $_end1;$newDate = date('Y-m-d', strtotime($_end. " + 1 month"));echo $newDate;?>">1 Month</option>
Now when if someone open browser and see my code he can replease 1 month with several month and that in MySQL.
How can I this secure and or hide that in HTML.
Thx
If you're wanting to have fields or input that can't be edited by the user, such as the current date that the form was submitted on or something along the lines of that, you need to do do all of that on the server side (not the client side). Any data that is submitted from the client side can (and you should treat it like it will) be changed.
Instead of having form fields with preset values, fields that are hidden, fields that are disabled, data that is rendered with JavaScript, or any other way you could think of storing data on the client side, do those things on the server side. You can use a PHP script to do this, seeing as you're already making use of PHP. When you submit the form it has to go to some sort of a server side script, do that logic there and submit that logic to a database.
filter all your received user input. This might be clear for free text inputs, but should be done as well for predefined values.
Easiest for extending might be to only accept a certain number. For example 1, 2 or 3.
$extend = filter_input(INPUT_POST, 'extend', FILTER_VALIDATE_INT); is the first step, but you should also check if $extend is not equal to an illegal number.
if(in_array($extend, range(1,3)){ }
input like numbers is a lot more simple to check than a range of dates.
But even when that would be needed: it is possible to make your own validation function.
It is not possible to limit the browser or the user to only send certain data in a form. Either they could use a tool in the browser to change the habits of a form element, or they could rebuild the form completely in their own htmlpage or other tool
There is very simple answer to your question - you can NOT secure html and you should not even try. Every browser is equipped with developer tools and even without browser anyone can send to your server whatever they want. This is how Internet works.
What you SHOULD do is to verify your input data on server side where user has no access. In your case you should have array of allowed inputs or function assessing if input from user is valid.
More, if you know what will be the algorithm eg. ($_end + 1month) than you do not need to get from user result but only value of $_end. You can calculate $newDate just before inserting data to database - this way user will have no way of changing it.
First of all, please be carefull with your writing, it is pretty hard to understand your problem.
Secondly, if you want to "hide" PHP code to the user, you could write your code in a different way :
You create a form in which users will be able to fill some informations, and for example a date, like in your example. If this date is an option, it can have some value, as the one you show.
Then when the user submit the file, you make a checking on the variables. If you want this form to show a price, to add some data to a database, or whatever, you do some checking to be sure that the values are correct. For example, if you want to calculate a price, you will check the date the user selected, and calculate the price from this date. With this method, even if user changed the code, they will not be able to change the checking (at least not easily).
And to conclude you show a page asking the user for confirmation. This way, he will check if the informations are correct, and you can ask to re-fill some fields if you detected some invalids values
That's hard to show some concrete code, since I don't really know what you want to do, but I hope this explanation was clear. Don't hesitate to ask some questions, I'll try to answer.
darling brother:
you have 3 method:
1: define a variables instead of 1 month
2: use encryption method for php enciding that provide encryption php cides to unformatted charachters (ionCube )
3: usin my sql encryption : MD5
What I'm trying to achieve is, if there is an non empty input + another input will show up.
This is how the input looks like.
<input type="file" name="image[]" />
What I'm trying to do is something like this
if (!empty($input)){
//Add another input
}
I think that you can get the idea, I am just wondering is this achievable, since I am new in php I don't know if this is possible.
And I'm sorry if I made any grammatical mistakes English is not my native language.
Sure! By looking at $_POST and $_FILES you can get an idea whether the input contained something or not. Then you can make that if and render an additional <input> as necessary.
Note though that PHP is server side code. It runs when the form is submitted in the browser and thus the browser makes a request to the server. If you want the additional input to appear immediately, as soon as the first input is filled, you'll need to use JavaScript. That's also quite possible.
My guess is, you want interactivity. In which case, you will need to use JavaScript, which is a client side language.
You hook an event to the input, and check each time it's changed to see if it's empty. If it is, you display another input.
You can see if the file was empty by using:
if ($_FILES['image']['size'] > 0)
Okay. So I don't have any example code to show, but after doing a bit of research and learning the basics of PHP, I think the answer to my question should be pretty simple.
Here is the scenario, as I would like it to be:
On the homepage there will be several team names, with scores next to them. Like "house-points" in Harry Potter.
Below the score is a small text-field. Below that is a submit button.
The user will put a number in the text-field, press submit, and that number will be added to the team's total score.
NOW. I know how to achieve all of that with JavaScript. Easy. What I want to know IS:
How do I make that new number (the new score total) STAY there. I need to permanently alter the HTML when that submit button is pressed.
All I know is that I need to use PHP. I know the basics of PHP, so whatever the answer is, just throw it at me and I'll figure it out.
Sounds like what you want to do is submitting forms. First drop the JavaScript, you won't need it. What you need is to put your text fields in a form and when you submit you can fetch your values with $_<GET|POST|REQUEST>['<name_of_field>'].
Then you will need to store it somehow. The best way to do it is to use a database like MySQL or MongoDB to store it, but it could be a bit tricky if you are just learning this, so maybe you would like to stick to files. You could do this with INI files and PHP's INI functions.
Lastly you will need to print out the correct values to the website. Now this is easy: Just edit your HTML file to do something like
<?php echo $score['team1']; ?>
for each team after retrieving the correct values at the top or something. (Don't forget to rename the HTML file to .php as well).
Now you should be all set to save your scores. =)
If you mean really permanent you'll have to send it to a database via Ajax (combination of PHP and Javascript). OR write it to a text-document, which is less good.
So, I'm pretty much a novice when it comes to programming and I would certainly appreciate a bit of help with an issue I'm having trouble getting my head around. Simply put, how do I get the selected variables from jquery.chained.remote.js back into my form for processing?
Here's the functioning, sort of, sample page.
http://www.noradaron.com/samplesearch/index.php
The dropdown list functions just fine but I just don't understand how to get the selected values back into my form. Yes, I will freely admit my knowledge is limited and I may not fully understand the answer once it is given. Regardless, I certainly appreciate any help I can get. Please let me know if there is anything else I could provide that would make my question more clear.
Attach submit event handler to your form like this:
$('form[name="search-vehicles"').submit(function() {
//code
});
Inside code grab the values like:
var value = $('#vType').val();
And update your text with the new values. However I recomment you to not write "textmore text" But to write your text inside *DIV*s or *SPAN*s with *ID*s so you can easily access them and change their values like:
<div id="selectedName"></div>
Then you can use:
$('#selectedName').html(value);
Beside putting id="vType" add name="vType" (and so on) so you can have your data sent back to server with either GET or POST .
I want to do the following but I want to find out if its possible and whats the easiest way to do it before I spend hours looking on google for something that isn't possible. The situation is a web app that would run of a users localhost so they can configure a config.php file.
Can the number of available check boxes be set in a config.php file. ie.
$numberofcheckboxes ="4"
Can descriptions be set for these in the config.php ie.
When these check boxes are displayed and ticked and then submitted can a php function be run with variables depending on which checkboxes were ticked. So in the config.php you would set the different variables for each checkbox. ie.
$check1name = "name 1"
$check1size ="120"
$check2name ="name 2"
$check2size ="160"
The function is always exactly the same function however the variables will be different for each checkbox.
Is this possible?
Thanks
I'm not quite sure I understand your final aim, but the ideas you describe sound feasible to me. However, I'd recommend defining the checkboxes and their details using an associative array, instead of as individual variables. Then you can use count($checkbox_array) to see how many checkboxes there are, and reference certain values using the number of the checkbox, like $checkbox_array[0]['name'].
Also, be aware of the difference between strings and integers. $numberofcheckboxes="4"; assigns a string, whereas $numberofcheckboxes=4; assigns an integer. Because PHP is loosely-typed you might not notice the difference in this case, but it is nonetheless an important difference to understand.
I think, you can use jquery...
Give your checkbox the same class, something like "chkclass". And give the value of checkbox in the value property. Then you can make a function that runs every click. Look at this code.
$(".chkclass").click(function(){
if($(this).is(":checked")){
//add your code here
//if you want to call some php with ajax, you can use .load function from jquery
}
});
I wish this is what you're talking about.. :)