I wanted to do that when my checkbox is checked, a php script whill be executed.
There is my code :
<form action="checkboxes.php" method="post">
<input type="checkbox" value="dawid" name="chk1"> 4K </input>
<input type="submit" name="Submit" value="Submit"></input>
</form>
<?php
$bdd=new PDO("mysql:host=localhost;dbname=techtronik.pl;charset=utf8", "root");
$wyszukiwanie = $bdd->query("SELECT * FROM procesory ");
while($wynik = $wyszukiwanie->fetch() )
{
echo $wynik ['nazwaproduktu'] . ". ";
}
?>
Can someone please help me ?
Thanks.
I am assuming you have basic knowledge of html, ajax, and javascript.
In your case you can use ajax call.
1) Checkbox checked
2) Call an ajax
3) Handle ajax and Run php code
4) Return to ajax
5) Success :)
Or just submit form on checkbox check then handle with php.
You can do it using jQuery, because it's easy solution for beginner.
Heres the sample jQuery code for your current code:
$('input[name=chk1]').click(function(){
if($(this).is(':checked')){
$('form').submit();
//or your ajax request
}
});
Why dont you use JavaScript/Ajax, jquery.... And call on check/ on click function
Related
Im coding php and i want to use image-button to call function.
$del= "<input type='image' src='delete.png' name='delete_cat' value='{$cat_name}' onclick='delete_exe(this.form,this.name,this.value)' >";
This button is into a submit form
<form method="post" action="caffe_menu_category_post.php">
/form>
My problem is that when js function(delete_exe()) call and execute, then have auto-submit.
I don't want that. Is there any solution or alternative code?
Can i set submit off for my button?
Thanks in advance.
ps in my code i submit form with submit input
input type = "submit" value = "Submit Your Entries">
Use
return false;
in the end of your delete_exe(); function. That will cancel submitting.
Ok. i find the solution. I change my post form
form name="myForm" method="post" action="caffe_menu_category_post.php" onsubmit="return validateForm()">
And use javascripts
var val=true;
function delete_exe(sel,kind,name){
//Set val false to stop submit
val=false;
}
function validateForm(){
if(!val){
//Stop submit
return false;
}
}
So the auto-submit stop.
On the oher hand when click submit button i call js function to set val=true;
i don't know if these is the best solution, but it's work for me!
I am making a exam portal in which i have option conducting exam/quiz. Now i have added 2 buttons namely Next and Previous . I wanted to fetch Question and its option on button Click.
My database has following structure: Question(qid,question,option1,option2,option3,option4,right_option)
What I am tryin to do:
<input id="first" type="button" value="NEXT" onClick = "show_next()">
<input id="second" type="button" value="PREV" onClick = "show_prev()">
<script>
function show_next()
{
<?php
$question_no; //my global php variable to keep track of question id
$question_no = $question_no + 1;
show_question($question_no); //php function which shows data according to question no
?>
}
function show_prev()
{
<?php
if($question_no>0)
{
$question_no = $question_no-1;
show_question();
}
else
{
?>
alert("Wrong Operation");
<?php
}
?>
}
</script>
I am new to php and javascript, please suggest the correct method and if possible coding snippet for my question
Use jQuery/AJAX.
All you have to do is manage Offset and Limit dynamically.
e.g.
lets consider you are showing 1 question at a time and its options.
your html file will be.
<input id="first" type="button" value="NEXT" onClick = "show_next()">
<input id="second" type="button" value="PREV" onClick = "show_prev()">
<input id="offset" type="hidden" value="0">
In your javascript file
function show_next()
{
var offset=$('#offset').val();
$.post('GetQuestion.php',{offset:offset},function(data){
$('#question_answer').html(data);
$('#offset').attr('value',offset+1);
})
}
function show_prev()
{
var offset=$('#offset').val();
$.post('GetQuestion.php',{offset:offset},function(data){
$('#question_answer').html(data);
$('#offset').attr('value',offset-1);
})
}
In your GetQuestion.php file you can access offset value using $_POST. All you have to do is use that value in your query.
mysql_query("SELECT * FROM questions LIMIT ".$_POST['offset'].",1");
echo your query result in php file so that it could be available to var data in javascript.
As far as i know, PHP is executed on the server and the JS is executed Client-Side, so you can't mix it.
You have to Options.
Load all Questions in different divs and hide them. Then you can show one by one with the next and the previous buttons.
Build the page with a parameter like this: http://myapp.com/question/1 And make the Next BUtton /question/2 The question page should now contain only 1 question.
Edit:
Here is a fiddle for the 1. method:
http://jsfiddle.net/zj7ts/
$('.question').hide()
$question_shown = $('.question').first().show();
$('.next').click(function(){
$('.question').hide()
$question_shown = $question_shown.next().show();
});
$('.prev').click(function(){
$('.question').hide()
$question_shown = $question_shown.prev().show();
});
id doesn't want to put the value in to the document.getElementById("UniLoc").value = val; and it doesn't want to submit the form how to fix this ?
This is how I call the function JS nas some value "asd56"
echo "
<script type="text/javascript">
repostD("'.$JS.'");
</script>";
here is my function inside
<script>
function repostD(val)
{
//document.getElementById("UniLoc").value = val;
document.getElementById('UniLoc').innerHTML = val;
//document.UlRL.UniLoc.value = val;
document.forms["UlRL"].submit();
}
</script>
and here is my form in html
<form name="UlRL" id="UlRL" action="in.php" method="post">
<input type="text" id="UniLoc" name="UniLoc" value="<?php echo $UniLoc;?>" />
Use
document.getElementById('UniLoc').value=val;
If there is nothing else wrong with your full form, it should submit. If you haven't closed your form tag, then close it.
</form>
try doint that in php all instead of in function make an if clause down the code that will execute the action of the posting of the form and at the beggining of the php try this
if($JS!="" )
{
$UniLoc=$JS;
$JS="something that will go throught the other if down at the code";
}
else {
$UniLoc=$_POST["UniLoc"];
}
I cant really tell from your question what is wrong, but looking at your code:
document.getElementById('UniLoc').innerHTML=val;
you are trying to set the value of the input with id 'UniLoc', but you are using the wrong syntax. Instead of .innerHTML, use .value
document.getElementById('UniLoc').value=val;
I have some data which will be displayed like this;
foreach ($holidays as $holiday)
{
$resultTable .= "<p>{$holiday->title}" . "<br/>" .
"{$holiday->pubDate}" . "<br>" .
"{$holiday->description}" . "<input type=\"checkbox\" name=\"saveCB\" value=\"3\"/>" . "<br /></p>";
}
Is there an easy way by which when the checkbox is clicked and the data would be added to a mysql table using AJAX?
Regards Darren
Yes you need javascript to do this. It can be done pretty easily though, if you are satisfied with the form submitting, and the page refreshing each time a select box is changed (i.e. check/unchecked). If you can't accept this, you'll have to use ajax. That would be your optimal solution, and easy as ajax is, it is a nice to have in your toolbox for future projects.
That said, you can achieve this by giving your form an id attribute, and paste this javascript just beneath your form (and edit the form id var):
<script type="text/javascript">
var formId = "YOUR FORM ID HERE";
function submitForm(){document.getElementById(formId).submit()}
</script>
Then add the following attribute to each checkbox: onchange="submitForm()".
Again, it is highly recommended to use ajax for this sort of stuff, and if you look into jQuery ajax, you'll be impressed how easy this can be done.
EDIT: What you can do to actually implement this in your existing code (replace it):
<form action="php-file-to-process-form.php" id="your-form-id" method="post">
<?php if(count($holidays)>0): foreach($holidays as $holiday): ?>
<p>
<?php echo $holiday->title; ?>
<br>
<?php echo $holiday->description; ?>
<input type="checkbox" name="saveCB[<?php echo $holiday->id; ?>]" value="<?php echo $holiday->id; ?>">
</p>
<?php endforeach; endif; ?>
</form>
<script type="text/javascript">
var formId = "your-form-id";
function submitForm(){document.getElementById(formId).submit()}
</script>
Please note i rewrote parts of your code. But in this case, assuming your $holiday objects has an "id" property, php-file-to-process-form.php should receive a fairly comprehensible post request.
PHP doesn't have onClick events, you would have to use JavaScript for something like that.. Or make it so you post your values with PHP (using a form), then it would be possible.
To avoid page refreshing with a form submit you'll want to use AJAX. You didn't tag your question as using jquery, but I highly recommend it. Here is a jQuery example of what you want:
$('input[type=checkbox]').click(function() {
if ($(this).is(':checked')) {
var name = $(this).attr('name');
var value = $(this).val();
$.post('/path/to/your/php/code', {name: value}, function(data){
//Handle the result of your POST here with data containing whatever you echo back from PHP.
});
}
});
Note that this puts the same click handler on all your checkboxes which might be the wrong assumption. If you have other checkboxes on your form that you don't want to use with this logic you'd just need to change the jQuery selector from 'input[type=checkbox]' to something more restrictive such as inputs that have a certain css class.
<input type="button" name="continue" id="continue" value="Continue" onclick="<?
if($_POST['rules']==null) {
echo "hello();";
}
elseif($_POST['rules']!=null) {
echo "myRedirect();";
}
?>" >
i have a form with a textarea. when the user hits the button when the textarea is null it should do the function hello() and if the textarea is not empty it should do the function myRedirect(). the function hello() and myRedirect() generates different dialog boxes. but it doesn't seem to work. what's wrong with this code?
It's not possible like that. JavaScript (onlick) executes in client browser while PHP executes on your server. IF you really need to execute PHP code you should use AJAX to send another request for your server to do PHP part.
If you're simply checking the value of the box, you just need Javascript... This should work fine:
<script type="text/javascript">
function check(obj_id){
var result = document.getElementById(obj_id).value;
if(result == ""){
is_null();
}else{
is_not_null();
}
}
function is_null(){
alert("null");
}
function is_not_null(){
alert("not null");
}
</script>
<input type="text" id="test" />
<button onclick="check('test')">Test</button>
Pass the id of the textbox you want to check into the check() function, and it finds whether anything has been entered into the box. If it has, it calls the function "is_null()" and "is_not_null()" if there is something in the textbox.
I think it will not work at all because you are trying to call a PHP code from javascript.
In other words PHP is a server side scripting language and you are trying to execute a server side code that needs to be submitted in order to get executed.
To achieve this functionality you can simply write this code
if($_POST['rules']==null)
{
echo "hello();";
}
elseif($_POST['rules']!=null)
{
echo "myRedirect();";
}
at the top of your PHP page and you will get what you want. or make the input type as "submit"
You probably need a postback to the server to execute whatever you want to run in the onclick event.
Another work around is to use AJAX.
Are you mixing up client side and server side? Is this what you're after?
<?php
if($_POST['rules'] == null) {
echo "hello";
} else {
myRedirect();
}
?>
<input type="submit" name="continue" id="continue" value="Continue">
<script>
function hello(){}
function myRedirect(){}
function ifnullz(el){
if($(el)!=0){
hello();
}else{
myRedirect();
}
}
function $(el){
document.getElementByID(el).length; //.value?? idk.. something like that.
}
</script>
<input onlcick="ifnullz('lolz');">
<textarea id="lolz"></ta>