0

I have two page one customform.php and another is preview.php.

I want to send some data which are values of some text-fields on customform.php using jquery post method. But I dont want the page to load. So I have used some JQuery code for this work. But now working for me.

the html code on customform.php is:

<form action="#" method="post">
<input type="text" value="" id="text1">
<input type="text" value="" id="text2">
<input type="button" value="preview" id="preview">
</form>

The jQuery code on customform.php is:

$('#previewph').click( function() {
    var v1 = $.('#text1').val();
        var v2 = $.('#text2').val();

 alert("Mail: " + v1 + " Message: " + v2);

$.post( "http://www.lexiconofsustainability.com/lex_tmp2/preview/" ,{ name : v1 , title :v2 });

  window.open("http://www.lexiconofsustainability.com/lex_tmp2/preview/", '_blank');
});

And on the preview.php I want to retrieve value form post method and echo them.

<?php 
echo  $authorname = $_POST['name'];
echo  $posttitle = $_POST['title'];
?>
user3777827
  • 344
  • 1
  • 5
  • 16
  • `$.post(URL, DATA, function(data) { console.log(data); })` <- Notice the **`third (3)`** options (`function(data){..etc`) ? That's to receive the response from the php script. – Darren Jul 08 '14 at 05:18
  • this is exectly what I did understand about function(data), can you please give me the code what I need to write in fuction(data){} for above example. – user3777827 Jul 08 '14 at 05:24
  • 1
    [**Read This**](http://stackoverflow.com/a/13657718/2518525) – Darren Jul 08 '14 at 06:00
  • first time on this post something is working for me.... I am working on it . On success I will inform you – user3777827 Jul 08 '14 at 06:54
  • @Darren I have put one question. Here[http://stackoverflow.com/questions/24628008/how-to-retreive-data-from-the-post-method-sent-by-ajax-jquery]. Can you please look at this. – user3777827 Jul 08 '14 at 09:37

2 Answers2

0

You can use AJAX for this..

The jQuery code on customform.php should be:

$('#previewph').click( function() {
    var v1 = $('#text1').val();
    var v2 = $('#text2').val();
$.post("preview.php", {name:v1, title:v2}, function(data)
{
   alert(data);
});
});

preview.php

<?php 
if(isset($_POST['name'])&&($_POST['title']))
{
echo  $authorname = $_POST['name'];
echo  $posttitle = $_POST['title'];
}
?>
Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
  • I also want to open the preview.php in new tab with the new data which is sent from the customform.php but the customform.php should not load. Please! – user3777827 Jul 08 '14 at 05:31
0

Simply Try this

  <form action="#" method="post" >
<input type="text" value="" id="text1">
<input type="text" value="" id="text2">
<input type="submit" value="preview" id="preview" onclick="senddata()" >
</form>
<div id="message"></div>

function senddata() {
var v1 = $.('#text1').val();
        var v2 = $.('#text2').val();

$.post("http://www.lexiconofsustainability.com/lex_tmp2/preview/", { name : v1 , title :v2  },
   function(data) {
      document.getElementById("message").innerHTML = data;    
   });

   }
Dinesh G
  • 244
  • 1
  • 13