<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>
Related
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
I have a button that runs a PHP function after the user onClicks. This is achieved using the POST method (as illustrated below). The user clicks on the button and the PHP function runs correctly, however, I want the user to be able to click the button multiple times and for the function to run multiple time. The function cannot over-ride its last result/outcome (so basically the result is echoed/printed each time the user clicks the button and the previous results are not overwritten).
Here is a section of my code, as follows:
<?php
function onClickArchive($detail_locator){
for ($x = 0; $x <= 34; $x++) {
echo "<li><br />";
kal_generator($detail_locator);
echo "</li>";
}
}
if(isset($_POST['load_more'])){
onClickArchive($detail_locator);
}
?>
</ul>
<div id="reload_section">
<center><br />
<form method="post">
<input type="submit" value="Load More" name="load_more" class="load_more_content" />
</form>
</center>
</div>
There are multiple methods to potentially solve this. One option would be to make the client send the existing data back when the button is pressed so you can append the new data to it without overwriting it.
The other option is to use AJAX to call the method and append to the existing data client side.
Use/Try this PHP Function:
function setInterval($f, $milliseconds)
{
$seconds=(int)$milliseconds/1000;
while(true)
{
$f();
sleep($seconds);
}
}
Usage:
setInterval(function(){
echo "I will echo many times!";
}, 1000);
It is like in setInterval in JavaScript
setInterval(Function(), seconds);
you need to make use of Jquery.
firstly use .load() method which will loads the data from server then use
.append() method which will add the resulting data to the selected element.
$('.load_more_content').click({
$('#reload_section').append($('<div />').load('sample.php'));
});
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 a column that has a button that when pressed, links to a URL set in PHP. I want to add a checkbox next to that button so that if it's checked when a user presses the button, it will take them to an alternate url. The PHP code setting the url:
<?php
$link = 'http://www.example.com';
?>
I realize that the code needs to be in javascript, which I don't know. I know only a tiny bit of php, so any help would be apprciated.
To clarify: (and of course I know this code will never work)
What I want to do is this:
<?php
If (checkbox is checked) {
$link = 'http://www.google.com';
} else {
$link = 'http://www.example.com';
}
?>
There is probably another way to do what you want to achieve. The value of the checkbox should be sent to a single php script on the server with the rest of the form's fields' values. Then you can use the checkbox's value (boolean) in php and do what you need to do accordingly, possibly requiring external scripts.
Checkbox value is not sent to server with form submit if it is not checked.
So, you can use something like this:
<?php
if (isset($_POST['checkbox_name'])) {
$link = 'http://www.google.com';
}else{
$link = 'http://www.example.com';
}
?>
Include the jQuery from Google:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
Then write the redirect function which you call after clicking the button;
<script>
function foo() {
if ($('#checkbox').is(':checked')) {
//redirect to google.com
window.location = "http://www.google.com/";
} else {
window.location = "http://www.example.com/"
}
}
</script>
And finaly your button should look like this:
<button onclick="foo();" >Your button</button>
This code assumes your checkbox has an id "checkbox".
Also, I don't think that what you're trying to do should be done with PHP - so you should learn Javascript/jQuery straight away instead of writing code the way it shouldn't be written.
Example: http://jsfiddle.net/5bdae/
Using jQuery this is fairly simple. You bind a function to the link, this function works out whether the checkbox is checked, if it is it links to one place, otherwise it links to another.
For an HTML structure like this:
<input id='myCheckbox' type="checkbox" name="box" value="box" />
<a href='#' id='myLink'>My Link</a>
The jQuery would be:
$('#myLink').click(function(event){
event.preventDefault();
if ($('#myCheckbox').is(':checked')){
window.location.href='http://www.example.com';
} else {
window.location.href='http://www.ask.com';
}
});
This could would go outside of the PHP tags, and you would need to include jQuery in your code.
How can I check a input field exists in the form when I submit it to the server?
For instance, I want to check whether a check box named 'mem_follow' exists or not in the form.
Or do I have to use javascript (jquery)?
I'm guessing you need to check on the server side after the form is submitted. If that's the case, you can check like so...
<?php
if (isset($_POST['mem_follow']))
{
// Work your server side magic here
} else {
// The field was not present, so react accordingly
}
?>
Hope this helps!
It'd HAVE to be Javascript. PHP can't reach out from the server into the browser's guts and check for you. It could only check if the fieldname is present in the submitted data.
In jquery it's trivial:
if ($('input[name="nameoffield"]')) { ... field exists ... }
Of course, this raises the question... why do you need to know if a field exists or not? Presumably you're the one who's built the form. You should know already if the field exists or not.
In the following script the form will not submit unless the checkbox is ticked. And If you do try, the hidden div with an error message is shown, to let you know why.
<form action="test.php" method="post" id="myform">
<input type="checkbox" name="checkbox" id="checkbox" value="YES" />
<input type="submit" name="submit" value="submit" />
</form>
<div id="error_message_div" style="display:none">You must tick the checkbox</div>
<script>
$('#myform').submit(function() {
if($('#checkbox').attr('checked')) {
return true;
} else {
$("#error_message_div").show();
return false;
}
});
</script>
Cheers
Matt
you can do this in two way:
By Server Side: In php
<?php
if (isset($_POST['mem_follow'])
{
// Work your server side magic here
} else {
// The field was not present, so react accordingly
}
?>
By Client side: In Jquery
$('#submit_button').click(function() {
if ($('input[name="box_name"]')) {
if($('input[name="box_name"]').attr('checked')) {
return true;
} else {
return false;
}
}
});
You can use jQuery and do something like this:
$('#myForm').submit(function (e) {
if($(this).children(':checkbox[name=mem_follow]').length > 0)
//exists
else
//doesn't exist
});
Or use php and check if there is any variable named 'mem_follow': (this is for POST, although it doesn't matter if it's GET or POST)
if(isset($_POST['mem_follow']))
//exists
else
//doesn't exist
You can try this code in the form submit event handler
if($("input[name=mem_follow]").length > 0){
//It exists
}
The other answers here are correct: you'd have to do that on the client side. In the case of a checkbox input field, if the box is not checked there is no guarantee the browser will include a POST parameter for that field.
For example, if you submit this form without checking the checkbox:
<form action="test.php" method="post">
<input type="checkbox" name="checkbox" value="YES" />
<input type="submit" name="submit" value="submit" />
</form>
only the submit=submit parameter will be submitted.
So, no, you cannot guarantee that a given checkbox exists in a form on the server side.
well for all elements try this
number = document.form_name.elements.length;
else for a specific name of input use this
number = document.form_name.getElementsBYName('Name of Input').length;
Thats it
enjoy
I suggest you to use Jquery
$.fn.Exists = function() {
return $(this).length>0;
}
if ($("here are your selector to check").Exists()) {
...
}
Simple and useful!
And you can use this Exists method everywhere))))
You can use javascript (or jQuery) to do that :
<script>
$('#myform').submit(function() {
if($('#yourFieldId').val()=='') {
alert('the field ' + $('#yourformField').name() + ' is empty !');
return false;
} else {
return true;
}
});
</script>
you can do this for all your fields one by one, or by putting all conditions in the if statement.