Sono un novellino in PHP e ho appena iniziato a imparare le basi di questo linguaggio.
Ho creato una pagina chiamata functioncalling.php che contiene due pulsanti, Invia e Inserisci. Come principiante in PHP, voglio testare quale funzione viene eseguita quando si fa clic su un pulsante. Voglio che l'output arrivi sulla stessa pagina. Così ho creato due funzioni, una per ogni pulsante. Il codice sorgente per functioncalling.php è il seguente:
<html>
<body>
<form action="functioncalling.php">
<input type="text" name="txt" />
<input type="submit" name="insert" value="insert" onclick="insert()" />
<input type="submit" name="select" value="select" onclick="select()" />
</form>
<?php
function select(){
echo "The select function is called.";
}
function insert(){
echo "The insert function is called.";
}
?>
Il problema qui è che non ottengo alcun output dopo che uno dei pulsanti è stato cliccato.
Qualcuno può dirmi dove esattamente sto sbagliando? Le risposte al più presto saranno molto apprezzate. Grazie in anticipo.
Il clic del pulsante è client side
mentre PHP è server side
, ma puoi ottenere ciò usando ajax
$('.button').click(function() {
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
Nel tuo file php:
<?php
function abc($name){
//your code here
}
?>
Sì, hai bisogno di un jax qui. Si prega di fare riferimento al codice qui sotto per maggiori dettagli.
Cambia il tuo markup in questo modo
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
jQuery: -
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'ajax.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert("action performed successfully");
});
});
});
In ajax.php
<?php
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'insert':
insert();
break;
case 'select':
select();
break;
}
}
function select() {
echo "The select function is called.";
exit;
}
function insert() {
echo "The insert function is called.";
exit;
}
?>
Dovresti fare in modo che il pulsante chiami la stessa pagina e in una sezione PHP controlla se il pulsante è stato premuto:
HTML:
<form action="theSamePage.php" method="post">
<input type="submit" name="someAction" value="GO" />
</form>
PHP:
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
{
func();
}
function func()
{
// do stuff
}
?>
non puoi chiamare le funzioni php come fare clic su un pulsante da HTML. perché HTML è sul lato client mentre PHP viene eseguito sul lato server.
O devi usare un po 'di Ajax o farlo come nel frammento di codice qui sotto.
<?php
if($_GET){
if(isset($_GET['insert'])){
insert();
}elseif(isset($_GET['select'])){
select();
}
}
function select()
{
echo "The select function is called.";
}
function insert()
{
echo "The insert function is called.";
}
?>.
Devi pubblicare i dati del modulo e quindi controllare il pulsante appropriato che viene cliccato.
Per mostrare $ messaggio nel tuo input:
<?php
if(isset($_POST['insert'])){
$message= "The insert function is called.";
}
if(isset($_POST['select'])){
$message="The select function is called.";
}
?>
<form method="post">
<input type="text" name="txt" value="<?php if(isset($message)){ echo $message;}?>" >
<input type="submit" name="insert" value="insert">
<input type="submit" name="select" value="select" >
</form>
Per usare functioncalling.php come file esterno devi includerlo in qualche modo nel tuo documento html
Prova questo:
if($_POST['select'] and $_SERVER['REQUEST_METHOD'] == "POST"){
select();
}
if($_POST['insert'] and $_SERVER['REQUEST_METHOD'] == "POST"){
insert();
}
Puoi scrivere come in javascript o jquery ajax e chiamare il file
$('#btn').click(function(){
$.ajax({
url:'test.php?call=true',
type:'GET',
success:function(data){
body.append(data);
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<form method='get' >
<input type="button" id="btn" value="click">
</form>
<?php
if(isset($_GET['call'])){
function anyfunction(){
echo "added";
// your funtion code
}
}
?>
Utilizzare una chiamata ricorsiva in cui l'azione modulo chiama se stessa. Quindi aggiungi il codice PHP nello stesso modulo per catturarlo. In foo.php
il tuo modulo chiamerà foo.php
su post
<html>
<body>
<form action="foo.php" method="post">
Una volta che il modulo è stato inviato, si chiamerà (foo.php
) e puoi prenderlo tramite la variabile predefinita PHP $_SERVER
come mostrato nel codice qui sotto
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "caught post";
}
?>
</form>
</body>
</html>
Ero bloccato in questo e l'ho risolto con il campo Nascosto
<form method="post" action="test.php">
<input type="hidden" name="ID" value"">
</form>
in valore puoi aggiungere qualsiasi cosa tu voglia aggiungere
in test.php puoi recuperare il valore tramite $ _Post [ID]
L'attributo onclick
in HTML chiama le funzioni Javascript, non le funzioni PHP.
Ecco un esempio che è possibile utilizzare:
<html>
<body>
<form action="btnclick.php" method="get">
<input type="submit" name="on" value="on">
<input type="submit" name="off" value="off">
</form>
</body>
</html>
<?php
if(isset($_GET['on'])) {
onFunc();
}
if(isset($_GET['off'])) {
offFunc();
}
function onFunc(){
echo "Button on Clicked";
}
function offFunc(){
echo "Button off clicked";
}
?>