-3

I have more than one problem. First one:

How can I send more than one data including JS functions:

$.ajax({
    type: 'POST',
    url: 'save.php',
    cache: false,
    data: {
        bla: navigator.appVersion,
        blah: navigator.platform
    }
});

Second problem is connected with first one:

How can I save more than one data:

<?php 
    foreach($_POST['data'] as $data) {
        $bla = $data['data1'];
        $blah = $data['data2'];

        $file = "test.txt"; 
        $fh = fopen($file, 'w') or die("can't open file");

        fwrite($fh, $bla, $blah);
        fclose($fh);
    }
?>
3
  • instead of foreach just use $_POST['bla']; and $_POST['blah']; Commented Mar 29, 2013 at 2:40
  • Hello if you can please post more enough from your code so we can detect where the problem persist because till now per example i don't have what you are posting as variable and what type of html element will carry out values and did you have many elemnt with same name or what ? if no so why this for each did you are trying to post a select element with multiple select options or what ? Commented Mar 29, 2013 at 2:46
  • Anyway i think that the first part of your problem solution's is found here : stackoverflow.com/a/15692090/1492486 and about the second part i am waiting you to post your form code that will carry out your data you want to post :) Commented Mar 29, 2013 at 2:50

3 Answers 3

1

Use $_POST['bla']; and $_POST['blah']; as your post variables.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use jQuery.param which serializes your objects/arrays.

var data = {
   object1: {version: navigator.appVersion},
   object2: {platform: navigator.platform},
   array1: ['one', 'two', 'three']
};
$.ajax({
      type: 'POST',
      url: 'save.php',
      cache: false,
      data: jQuery.param(data),
});

and you can see the data structure in php and parse acordingly:

<?php 
    print_r($_POST);
?>

Comments

0

Marhaba ! Assuming that you have just two input fields in your form you want post so what you need is just this type of code :

<?php
$tobewritten = "";
foreach($_POST as $data)
{
    $tobewritten.= $data . ",";
}
$tobewritten = substr($tobewritten,0,strlen($tobewritten)-1);

$file = "test.txt"; 
$fh = fopen($file, 'w') or die("can't open file");

fwrite($fh,$tobewritten);
fclose($fh);
?>

Please 5alline a3ref if this code has helped you and has been the right solution for your problem :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.