user6665903
user6665903

Reputation:

Execute php using jquery post

I've tried to go to php file using jquery.

Here is my code.

This is index.php

$.post('test.php',data,function(json){},'json');

This is test.php

//set session variable from passed data
$_SESSION['data1'] = $_POST['data1'];

<script>
    window.open('test1.php','_blank');
</script>

This is test1.php

echo $_SESSION['data1'];

But this code is not working. I want to pass data from index.php to test1.php.

How can I do this? I don't want to use GET method because of too long url.

Anyhelp would be appreciate.

Upvotes: 1

Views: 59

Answers (3)

RryLee
RryLee

Reputation: 555

I like use jQuery post a url like this.

$('form').on('submit', function(e) {
  e.preventDefault();

  var $this = $(this);

  $.ajax({
    url: $this.attr('action'),
    method: $this.attr('method'),
    data: $this.serializeArray(),
    success: function(response) {
      console.log(response);
    }
  })
});

I you a beginner, you can reference this project

php-and-jQuery-messageBoard

Upvotes: 0

PHP Geek
PHP Geek

Reputation: 4033

I am not quite clear from you explanation right now. But I am here trying to resolve you problem as you can use the jquery post method as follows :

$.post('test1.php',{param1:value1,param2=value2,...},function(data){
 //Here you can take action as per data return from the page or can add simple action like redirecting or other 
});

Here is a simple example of register :

$.post('', $("#register_form").serialize(), function(data) {
  if (data === '1') {
    bootbox.alert("You have registered successfully.", function() {
      document.location.href = base_url + '';
    });
  } else if (data === '0') {
    bootbox.alert("Error submitting records");
  } else {
    bootbox.alert(data);
  }
  $("#user_register_button").button("reset");
});

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Try this:

$.ajax({
    url: 'test.php',                        
    type: 'POST',                                  
    data: {
        myData : 'somevalue'
    },
    success: function(response){    // response from test.php
        // do your stuff here
    }
});

test.php

$myData = $_REQUEST['myData'];
// do your stuff here

Upvotes: 0

Related Questions