-1

I have 3 textboxes and i can't get it value with $_GET and $_POST options. I need something like this: -I fill textboxes with text -I click button -I activate function when clicking button -In this function I recieve text from textboxes -I save it to file (saving to file works ;) )

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>One Hit Point</title>
<link href="Bez_nazwy1.css" rel="stylesheet">
<link href="test.css" rel="stylesheet">
</head>
<body>
<form action="<?php $_PHP_SELF ?>" method="GET">
<label for="" id="Label1" style="position:absolute;left:67px;top:16px;width:194px;height:31px;line-height:31px;z-index:0;">Imię</label>
<input type="text" id="x11" name="x1" style="position:absolute;left:158px;top:23px;width:196px;height:16px;z-index:1;" value="">
<label for="" id="Label2" style="position:absolute;left:67px;top:63px;width:289px;height:31px;line-height:31px;z-index:2;">Nazwisko</label>
<input type="text" id="x22" name="x2" style="position:absolute;left:158px;top:70px;width:196px;height:16px;z-index:3;" value="">

<label for="" id="Label3" style="position:absolute;left:67px;top:111px;width:194px;height:31px;line-height:31px;z-index:5;">Telefon</label>
<input type="text" id="x33" name="x3" style="position:absolute;left:158px;top:118px;width:196px;height:16px;z-index:6;" value="+48 ">
<input type="button" id="Button1" onclick="<?php costam() ?>;" name="" value="Zatwierdź" style="position:absolute;left:194px;top:164px;width:96px;height:25px;z-index:7;">
</form>
</body>

<body>
<?php
//window.location.href='./niespodzianka.html';return false;
function costam(){
    $imie= $_GET['x1'];
    $Nazwisko=$_GET['x2'];
    $Numer= $_GET['x3'];
    //łączenie
    $dane=$imie.", ".$Nazwisko.", ".$Numer;
    //dodawanie nowej linii
    $popr=$dane." k\n";
    //otwieranie, zapisanie, zamknięcie
    $plik=fopen("zap.txt", "a");
    fputs($plik, $popr);
    fclose($plik);
}
?>
</body>
</html>

EDIT:

new code (now there are 2 files): File 1 (dz.php):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>One Hit Point</title>
<link href="Bez_nazwy1.css" rel="stylesheet">
<link href="test.css" rel="stylesheet">
</head>
<body>
<form action="pliki.php" method="GET">
<label for="" id="Label1" style="position:absolute;left:67px;top:16px;width:194px;height:31px;line-height:31px;z-index:0;">Imię</label>
<input type="text" id="x11" name="x1" style="position:absolute;left:158px;top:23px;width:196px;height:16px;z-index:1;" value="">
<label for="" id="Label2" style="position:absolute;left:67px;top:63px;width:289px;height:31px;line-height:31px;z-index:2;">Nazwisko</label>
<input type="text" id="x22" name="x2" style="position:absolute;left:158px;top:70px;width:196px;height:16px;z-index:3;" value="">
<label for="" id="Label3" style="position:absolute;left:67px;top:111px;width:194px;height:31px;line-height:31px;z-index:5;">Telefon</label>
<input type="text" id="x33" name="x3" style="position:absolute;left:158px;top:118px;width:196px;height:16px;z-index:6;" value="+48 ">
<input type="button" id="Button1" onclick="location.href='pliki.php'" name="" value="Zatwierdź" style="position:absolute;left:194px;top:164px;width:96px;height:25px;z-index:7;">
</form>
</body>
</html>

File 2 (pliki.php):

<?php
//window.location.href='./niespodzianka.html';return false;

$imie= $_GET['x1'];
$Nazwisko=$_GET['x2'];
$Numer= $_GET['x3'];
//łączenie
$dane=$imie.", ".$Nazwisko.", ".$Numer;
//dodawanie nowej linii
$popr=$dane." k\n";
//otwieranie, zapisanie, zamknięcie
$plik=fopen("zap.txt", "a");
fputs($plik, $popr);
fclose($plik);
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Bez nazwy</title>
<meta name="generator" content="WYSIWYG Web Builder 14 - http://www.wysiwygwebbuilder.com">
<link href="Bez_nazwy1.css" rel="stylesheet">
<link href="niespodzianka.css" rel="stylesheet">
</head>
<body>
<div id="wb_TextArt1" style="position:absolute;left:16px;top:13px;width:867px;height:470px;z-index:0;">
<img src="images/img0001.png" id="TextArt1" alt="Bede dzowni&#263; xd" title="Bede dzowni&#263; xd" style="width:867px;height:470px;"></div>
</body>
</html>
icy
  • 1,468
  • 3
  • 16
  • 36
jasiekk
  • 3
  • 2

1 Answers1

0

That code can't works. You can't call PHP function using javascript onclick. For your clarification this code would execute costam() function everytime page is loaded.

Try to change Button1 with:

<input type="submit" id="Button1" name="submit" value="Zatwierdź" style="position:absolute;left:194px;top:164px;width:96px;height:25px;z-index:7;">

Then change php code in:

<?php
if($_GET['submit']){
    $imie= $_GET['x1'];
    $Nazwisko=$_GET['x2'];
    $Numer= $_GET['x3'];
    //łączenie
    $dane=$imie.", ".$Nazwisko.", ".$Numer;
    //dodawanie nowej linii
    $popr=$dane." k\n";
    //otwieranie, zapisanie, zamknięcie
    $plik=fopen("zap.txt", "a");
    fputs($plik, $popr);
    fclose($plik);
}

This should works, but is not a good approch. Also the page will refresh.


If you're familiar with jQuery:

jQuery.ajax({
type: "POST",
url: 'your_functions_address.php',
dataType: 'json',
data: {functionname: 'add', arguments: [1, 2]},

success: function (obj, textstatus) {
              if( !('error' in obj) ) {
                  yourVariable = obj.result;
              }
              else {
                  console.log(obj.error);
              }
        }
});

This may help you: Call php function from JavaScript

icy
  • 1,468
  • 3
  • 16
  • 36