Run a php function when click on a button - php

I have a php script page with a form like this:
<form method="post" action="clientmanager.php">
<input type="text" name="code_client" id="code_client" />
<input type="submit" value="Save" />
</form>
In my file "clientmanager.php", I have a function for example "addClient()".
I want to click the button and only call the function "addClient()" in the file "clientmanager.php" instead of call the whole file "clientmanager.php", So how could I do??
Thx!!!!

Add this to the top of the file:
if (isset ($_POST ['code_client'])) addClient();
However, you should consider using a different setup - processing forms like this is considered bad practice.
Maybe create a separate file, use OOP, MVC, a framework, anything other than this.

You can do that over jquery (ajax).
Call jquery library in head
Call clientmanager.php over this code:
my_form.php
....
<script type="text/javascript" src="jquery.js"></script>
...
<form method="post" action="">
<input type="text" name="code_client" id="code_client" />
<input id="my_button" type="button" value="Save" />
</form>
<div id="response_div"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#my_button").click(function(){
$.post("clientmanager.php", {code_client: $('#code_client').val()}, function(data){
if(data.length >0) {
$('#response_div').html(data);
}//end if
});
});
});
</script>
clientmanager.php
<?php
echo $_POST['code_client'];
?>

Related

Run a PHP script on click of a button?

I have a php file which I want be executed when I click on a button. I have been using the following code to achieve the same:
<form action="test.php" method="post">
<input type="submit" value="Run Script" name="submit">
</form>
The code seems to take me to test.php file, but I want to be redirected back to the page where I was. Can is be possible to run this test.php on click on a button and not get redirected? Something like running the script in background?
Please let me know if you guys need further clarification.
Any help will be highly appreciated.
Thanks in advance guys!
you will need to use AJAX
<script>
function doTheFunction(){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","URL_OF_PHP_FILE",true);
xmlhttp.send();
}
</script>
<button onclick"doTheFunction();">Run the script</button>
You need to do an ajax call and prevent the forms submit.
Html:
<form onsubmit="return doAxajCall();"> </form>
js Code:
function doAjaxCall(){
//do ajax call however you want
return false; //prevents the form from submitting
}
Easy way;
Make the action of your form 'self' like this;
<form action="#" method="post">
<input type="submit" name="submit" value="Do PHP">
</form>
Then test for submission at the top of the page like this;
if (isset($_POST['submit'])) {
// do stuff
}
in your instance 'test.php' has to included in the original file.
at the bottom of you php file
header('Location: lastPage.php');
or do some tricks using ajax.
code:
<!-- including jquery.min.js cdn -->
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<input type="submit" value="Run Script" id="submit" name="submit" />
<!-- now set ajax call for button -->
<script>
$("#submit").click(function(){
$.ajax({
url:'test.php',
type:'POST',
success:function(response){
alert('test.php file executed');
}
});
});
</script>

ajax post javascript variable to php in same page

i write sample code to ajax post javaxcript varible to php in 2 page, test.php and validate.php.
test.php :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="jquery.min.js" ></script>
</head>
<body>
<form>
<input type="text" id="name" placeholder="enter Your name...." /><br/>
<input type="text" id="age" placeholder="enter Your age...." /><br/>
<input type="button" value="submit" onclick="post();">
</form>
<div id="result" ></div>
<script type="text/javascript">
function post()
{
var name=$('#name').val();
var age=$('#age').val();
$.post('validate.php',{postname:name,postage:age},
function(data)
{
if (data=="1")
{
$('#result').html('you are over 18 !');
}
if (data=="0")
{
$('#result').html('you are under 18 !');
}
});
}
</script>
</body>
</html>
validate.php
<?php
$name=$_POST['postname'];
$age=$_POST['postage'];
if ($age>=18)
{
echo "1";
}
else
{
echo "0";
}
?>
how can i write/change above code in same page ?? and validate.php insert in test.php ??
added after first answer :
"but i dont have/use any button or in my page.
and where can i insert validate.php code in test.php. please help by insert complete correct code."
Try adding a submit() method to your <form> rather than attaching the event to the submit button.
$(document).on('submit', 'form', function(){
post();
return false;
});
At the moment I think your form is submitted no matter what, you have to either return false on submit or preventDefaults()
A good idea is also to add an ID to your form in case you have more than one on your page and use the relevant selector in the .on event handler such as
$(document).on('submit', $('#form-id'), function(){ ... });
ajax is use for exchanging data with a server. If you want write code in same page so reload page and write validate.php code above of test.php like this
<?php
//validate.php code
?>
<form action=test.php type=POST>
<input type="text" id="name" placeholder="enter Your name...." /><br/>
<input type="text" id="age" placeholder="enter Your age...." /><br/>
<input type="button" value="submit">
</form>

Redirecting URL form

I'm starting to study web development and as exercise I was trying to make a form for redirecting in HTML with javascript and PHP.
HTML
<html>
<head>
<title>FORM</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="script.js" type="text/javascript"></script>
<form>
<input type="text" name="url"><br>
<input class="btn" type="button" id="send" value="Send" />
</form>
</body>
</html>
JavaScript
$(document).ready(function() {
$("#send").click(function(){
var url = $("#url").val();
$.ajax({
type: "POST",
url: "engine.php",
data: "url=" + url,
dataType: "html"
});
PHP
<?php
session_start();
$url = $_POST['url'];
header("Location: /".$url.");
session_destroy();
exit;
?>
The main problem is that seems that the js isn't read. Futhermore I don't know if the PHP is correct. What's wrong?
There's no need to make an AJAX request if you want to redirect to another URL. Also, the way your doing it at the moment wouldn't redirect anyway.
Below, when your click function fires, it will get the URL from your input field, and re-direct to that page.
<html>
<head>
<title>FORM</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form_name").submit(function(event){
event.preventDefault();
var url = $("#url").val();
document.location.href = url;
});
})
</script>
<form id="form_name" name="form_name">
<input type="text" name="url" id="url"><br>
<input class="btn" type="submit" id="send" value="Send" />
</form>
</body>
</html>
you need to add id to input
<input type="text" name="url" id="url"><br>
and also header() don't work in ajax
you need to use javascript for redirect
in php
<?php
session_start();
$url = $_POST['url'];
echo '<script>document.location.href = '.$url.'</script>';
session_destroy();
exit;
?>
and put echo text into document with ajax respond function
also you can use a better way ( with out Ajax and submit form )
$(document).ready(function() {
$("#send").click(function(){
var url = $("#url").val();
window.location.href = url;
});
here is the best answer for you How to redirect to another webpage in JavaScript/jQuery?
UPDATE:
also you need delete , tags, here the html code instead of ...
<input type="text" name="url" id="url"><br>
<input class="btn" type="submit" id="send" value="Send" />
So you think your javascript isn't being executed? Does that mean you've debugged this?
If none of your javascript runs - including your ready handler, then the server isn't finding the js files. If some of your javascript is running it's possible that an error is causing it to stop. You probably just need to do a little debugging.

How to read all the values inside an multiple form with jQuery

I'm trying to get my uploading script to work with jQuery but having problems with fetching the values (files) that are queued up in a multiple form.
I can get it to work so I can select like 10 files in a single input but when I'm trying to fetch those values I only get the first file of the 10 I added simultaneously. I can upload the files and fetch its values but I want to make it to work with jQuery as well something I can't get to work.
Here is the code:
<!DOCTYPE html>
<html lang="en-us">
<head>
<script src="jquery-min.js"></script>
<script>
$(document).ready(function() {
$("form").change(function() {
var form = $(".forms").val();
$(".files").append("Files:"+form);
});
});
</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" class="forms" value="" name="upload[]" multiple>
<button type="submit">Upload!</button>
</form>
<div class="files"></div>
<?php var_dump($_FILES);?>
</body>
</html>
So when I drag and select the files and adds them then only the first value gets assigned to "div.files". So my question is how do i read the array of the files inside of it so I just don't get the first one?
Here is an image that displays the problem: http://i.stack.imgur.com/lOAvk.png
I recommend a jQuery uploader plug-in like: Upoadify. It has a nice UI out-of-the-box w/ a progress bar and probably offers a better API for your code above. Here's an example:
<script type="text/javascript">
$(function() {
$('#file_upload').uploadify({
'uploader' : '/path/to/uploadify.swf',
'script' : '/path/to/uploadify.php',
'cancelImg' : '/path/to/cancel.png',
'folder' : '/uploads',
'onAllComplete' : function(event,data) {
document.getElementByID('someForm').submit();
}
});
});
</script>
<form id="someForm" action="someFile.php" method="post">
<input name="someField" type="text" />
<input id="someID" name="someName" type="file" />
<input onclick="$('#someID').uploadifyUpload()" type="button" value="Submit" />
</form>

php output display on a greybox

I have a form that calls a php script on submit to insert data into a MySQL database. I would like the output of the php script return to a greybox. I haven't been able to make it work so I appreciate any help you guys can provide.
I have the greybox call on the form definition see below but is not doing the trick.
Here is a subset of the code:
<script type="text/javascript" src="greybox/AJS.js"></script>
<script type="text/javascript" src="greybox/AJX_fx.js"></script>
<script type="text/javascript" src="greybox/gb_scripts.js"></script>
<div id="content">
<form id="contact_us" name="contact_us" action="contact-greybox.php" method="POST" onSubmit="return GB_showCenter('Testing', this.action, 500, 500)">
<fieldset>
<label for="employee_id">Employee ID:</label>
<input id="employee_id" name="employee_id" type="number" size="10" /><P />
<label for="employee_name">Employee Name:<strong><br /> (as it should appear on
email) </strong></label>
<input id="employee_name" name="employee_name" type="text" /><P />
</fieldlist>
<p class="submit"><input type="image" name="submit" value="Submit Form" src="icons/ambas_submit.jpg" boder="0">
</form>
</div>
The php is a simple insert statement into MySQL.
Appreciate any help
greybox doesn't support POST submits, but the general pattern is to use ajax to submit the form- otherwise your page will refresh.
You need to set an onclick( $.submit ) to the form input then return false at the end of your ajax call:
$('#contact_us').submit(function(){
//get your inputs here
var e_id = $.('#employee_id').val();
//...etc....
$.post( ...
//set your data/input fields here:
data: { id: e_id },
success: function(response){
//display the response: this is what you get back from: contact-greybox.php
}
})
return false;
});
fancybox is an overlay box that supports being called with pure html as a parameter, so that you can just put this in your success function:
$.fancybox(response);
...or
$.fancybox(response.html)... etc.

Categories