Jump to content

Recommended Posts

I'm using ajax to preview post before submit. But there is problem. I have in <form action javascript (for preview post) instead of submit.

 

Anyway here is my code:

 

HTML (Ajax):

<script type="text/javascript" language="javascript">
   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
             // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('myspan').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "reply=" + encodeURI( document.getElementById("reply").value );
      makePOSTRequest('preview.php', poststr);
   }
</script>

 

Here goes preview:

<span name="myspan" id="myspan"></span>

 

Form:

<form name="myForm" method="post" action="javascript:get(document.getElementById('myform'));">

<textarea class="reply" id="reply" name="reply" style="height:260px;width:680px;"></textarea>

<input type="submit" class="inputButton" value="(SUBMIT_REPLY)" />

 

preview.php

<?php
$mytext = $_POST['reply'];
echo "Preview: <hr>".$mytext."<hr>";
?>

I have only preview form, but cant submit post.

 

Before I implement this preview script my form was:

<form name="myForm" method="post" onsubmit="doCheck()" action="reply.php?t={T_ID}&page={PAGE}">

 

How to have submit and preview buttons in this post form?

I solve it...Now I got two forms. For preview and for submit.

<form name="myForm" method="post" onsubmit="doCheck()" action="reply.php?t={T_ID}&page=(PAGE)"> 

...then <textarea ....</form>

 

and after:

<form name="myForm" method="post" action="javascript:get(document.getElementById('myform'));">
<input type="submit" class="inputButton" value="Preview" />

 

But it looks ugly, cause buttons are not inline, but in new line:

<SUBIT>

<PREVIEW>

 

I want to be: <SUBMIT> <PREVIEW>

:)

why not just do

 

    <input type="submit" value="Preview" name="preview" class="signsub" id="preview" onClick="showPreview(); return false"/>

      <input type="submit" name="submit" id="submit" value="Submit" />

 

one form, 2 buttons....  add 10 more that way if you want -- it's all in the way you handle the processing of those buttons.

 

also when using ajax, you don't need an action because the action should be in the ajax itself or passed into the ajax through a parameter.

Ok, now its working, but I want to add some cosmetics.

 

Loading image preview or whatever would be nice and I have tried to implement it, but its not working. Image is loaded always after post content.

 

How to display image (ex. loading.gif) on mouse click, before preview content appear?

  • 4 weeks later...

Ok I solve it, but now I noticed when I insert "&" character preview failed.

Here is code:

<script type="text/javascript" language="javascript">
   var http_request = false;
   function makePOSTRequest(url, parameters) {
   document.getElementById('myspan').innerHTML = '<div align="center"><img src="img/loader.gif" /></div>';
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
             // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('myspan').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "reply=" + encodeURI( document.getElementById("reply").value );
      makePOSTRequest('preview.php', poststr);
   }
</script>
    <span name="myspan" id="myspan"><a name="myspan" href=""></a></span>

 

I have tried to filter it in preview.php:

$mytext = ($_POST['reply']);

$mytext = htmlentities($mytext);

or

$mytext = htmlspecialchars ($mytext);

But still preview fail.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.