views:

2161

answers:

4

Hello!

I have an C# cart application that needs to POST some data to a PHP page and redirect the user to that page to view the data. Everything is working fine! So, what is the problem??

Since we are using a Javascript function to POST the form to the PHP page through setting its action to the PHP URL, it is not allowing us to clear our Session variable with our cart contents.

Once the user clicks checkout and is sent to the third party site, we want our session variable that stores their cart contents to go away. To my knowledge I cannot clear this via the Javascript, so my idea was to send the POST data and the user to the PHP page through the C# code.

When the user clicks checkout, the Javascript reloads the page, sets the cart data to a string variable, clears the session, then POSTs the data and sends the user to the PHP page.

All of this is working, except for the POST of data and redirecting the user. Unfortunately, the third party page cannot accept a URL.PHP?=var type parameter for security reasons, so we have to POST it.

Using WebRequest I believe I can get the data posted, but I cannot get the user redirected to that page to finish out their order. Any ideas?

A: 

You can keep on using the Javascript solution and just add a Ajax call that will abandon the session

CD
I've never used Ajax - do you have a reference for this or a way to explain the idea? Thanks for the response!
Well you don't really need AJAX (asynchronous JavaScript and XML), you need just an or asynchronous JavaScript call. I suppose you can start with searching for "asp.net webmethod" or "asp.net ajax".
CD
I'll do some more looking around - I had tried finding a way to clear the session from within the Javascript function, but didn't get anywhere. I'll search on asynchronous calls and see where that gets me.
If you were trying to solve this with a javascript function you might have been a bit confused between client-side and server-side.Sessions are saved on the server, while javascript is on the client.
CD
Yeah, I realize that. Thus the dilemma. We created this application without thinking that the user would ever want to go back to the original page, which most won't. They will checkout, pay, close their browser or the session will time out. Then we realized that someone may place one order and decide to place another - which then we would need to have the session variable cleared on checkout, but we have to do it without redesigning everything. So - if we can figure out how to clear it by calling a server-side function from the client-side Javascript - it'll be a win.
A: 

I recommend you implement an intermediary page to prepare the data and cleanup the Session for you. The 'checkout' link would simply navigate the user to this intermediary page, which would do the following:

  1. Collect the user's cart data out of the session
  2. Clear the session
  3. POST to the PHP page using WebRequest

From the MSDN on WebRequest:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main ()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}
kenleycapps
Hey Kenley! I actually just worked that up and know that WebRequest will do the trick for posting the data to the php page - however, the user has to be redirected to the page that the data is posted to, since the data is used to finalize their checkout. Is that possible with WebRequest? To not only POST the data but to send the user to the same page to see the data?
A: 

Hi thanks for the well commented code, I am not getting redirected to the other forms server (it is another site) an suggestions?

WASSA
A: 

I am only speculating here but you should be able to transmit the data in a WebBrowser control item, that way it would send the post data and redirect.

Paramiliar