Reputation: 141
i have a file with php function, called function_query.php
function_query.php
<?php
$connection = mysqli_connect( "localhost","root","","mydb");
function query1($connection){
//my query
}
function query2($connection){
//my query
}
?>
Then in another file called mypage.php i want post some data with jquery $.post , but i dont know how to call the spesific function with jquery $.post , and my script is like this
mypage.php
<?php include_once('function_query.php')?>
<button id="btn-save"> Save </button>
<script type="text/javascript">
$('#btn-save').on('click',function(e) {
$.post(base_url + "function_query.php/query1", {}, function(res) {
alert('Save!');
});
});
</script>
When i run that script, it's doesn't error but my data isn't save to my database
How would i call that function in javascript?
Thank you
Upvotes: 0
Views: 91
Reputation: 2138
You are using function.php/query1 but your file name is function_query.php
Try this
$('#btn-save').on('click',function(e) {
$.post(base_url + "function_query.php/query1", {}, function(res) {
alert('Save!');
});
});
and Also remove $connection from function and use that inside the function
<?php
$connection = mysqli_connect( "localhost","root","","mydb");
function query1($data){
//my query
}
function query2($data){
//my query
}
?>
You can also try this
<script>
$.ajax({
url:base_url + "function_query.php/query1",
type:'post',
dataType:'json',
contentType:'application/json',
data:JSON.stringify({your data will goes here })
success:function(data){
console.log(data);
},
error:function(err){
console.log(err)
}
})
</script>
Upvotes: 0
Reputation: 543
just try this
<button id="btn-save"> Save </button>
<script type="text/javascript">
$('#btn-save').on('click',function(e) {
$.post("test1.php?func=query1", {a:'abc'}, function(res) {
alert(res);
});
});
</script>
and php looks like this
$_GET['func']();
function query1(){
//my query
echo $_POST['a'];
echo 'yyy';
}
function query2(){
//my query
echo 'nnn';
}
echo 'no';
Upvotes: 0
Reputation: 4794
You can add condition in your php file and get data from javascript such as $_POST['query_type'] in php file and then you can call your functions to save data on database.
your function_query.php file:
<?php
function query1($data){
//your query
}
function query2($data){
//your query
}
$query_type = $_POST['query_data'];
$data['name'] = $_POST['name'];
$data['family'] = $_POST['family'];
if($query_type == 'insert'){
query1($data);
} else {
query2($data);
}
?>
this can be your jquery code:
$.post( "function_query.php", {
query_data: "insert",
name: "Muhammad",
family: "Rifqi"
});
Upvotes: 2
Reputation: 178
you need to handle the request for function call.
function_query.php
<?php
$connection = mysqli_connect( "localhost","root","","mydb");
if(isset($_REQUEST['query1']))
query1();
else if(isset($_REQUEST['query2']))
query2();
function query1(){
//my query
}
function query2(){
//my query
}
?>
and in Your mypage.php put filename.php?functionname.....
mypage.php
Save
$('#btn-save').on('click',function(e) {
$.post(base_url + "function_query.php?query1", {}, function(res) {
alert('Save!');
});
});
Upvotes: 0