Jump to content

Recommended Posts

Hello,

 

I'm trying to create aspx's postback effect in PHP. This can be done with javascript with the use of a hidden "viewstate" (aspx generated) variable. Viewstate preserves the state of a page (input text in text boxes stay the same) when a form action has been submitted.

 

So what I'm trying to preserves inputted/selected text and display new information based on that inputted/selected text when a form action has been submitted.

 

Here is basic example for what I want to recreate in PHP (generated from aspx):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Untitled Page
</title></head>
<body>
    <form name="form1" method="post" action="default.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUINTEwNDE4MTBkZEUojnNucBpb9SAjxDe4Z6ZI/m7G" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBQKH453zCwKd5I/lCgKxx4CJAgLlwPXgBwKO7fe5AhVWST46gYYuNp7aSlVEEFAP5t5Y" />
</div>
    <div>
    
        <select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">
<option selected="selected" value="first">first</option>
<option value="second">second</option>

<option value="third">third</option>

</select>
    
    </div>
    </form>
</body>
</html>

 

Thanks

well yeah I could do that but I don't want to reload the page. I'm looking to only refresh one part of the page for the information selected. I need javascript to do it but there is a part missing that I don't know how to do.

 

Here is an example of what I'm trying to do. Just select different items in the dropdown-list to see what I'm talking about

 

http://demo.koolphp.net/Examples/KoolComboBox/Advances/AutoPostback/index.php

It's just a simple JavaScript form submission that fires on the <select> element's onchange event.  To do it without a page refresh, use AJAX during the event instead of a 'normal' submission.

 

And, just to be clear about ASP.NET's postback/viewstate capabilities, postback works almost exactly how thorpe's suggestion works.  Hidden inputs are injected into the page.  The viewstate is simply a base-64 encoded string containing the values of the server control properties on the current page.  This encoded viewstate meta-value is set as the value for one of the hidden inputs, where it is decoded and parsed on the back end during page rendering.  For more, see: http://msdn.microsoft.com/en-us/library/ms972976.aspx

 

You can do essentially the same thing with PHP and hidden inputs, with the added benefit of not having to concern yourself with ASP.NET's horrid page lifecycle.

Here is my attempt at getting this working. So far no luck.

I'm just gonna post the relevant code.

 

<script>
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}


function searchDate(strURL) {		

	var req = getXMLHTTP();

	if (req) {

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('recordTable').innerHTML=req.responseText;						
				} else {
					alert("An error occurred:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}
}
</script>

<form action="" method="post">
<p>Date:<input type="date" name="selectedDate" size="10" />
<input type="submit" name="submit[searchDate]" value="Recherche" onclick="searchDate('planification.php?selectedDate='+document.getElementById('selectedDate').value)" style="margin-right: 25%;" />
<div id="recordTable">
</div>

 

planification.php

        $stmt->bind_param("s",$selectedDate);
$stmt->execute();
//variables are binded and displayed in the same order as the select query
$stmt->bind_result($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o); 

    $planificationTable = '<table border="1"><th>a</th><th>b</th><th>c</th>
<th>d</th><th>e</th><th>f</th><th>g</th><th>h</th><th>i</th>
<th>j</th><th>k</th><th>l</th><th>m</th><th>n</th>
<th>o</th>';
    
while($stmt->fetch())
	$planificationTable .= '<tr><td>' . $a . '</td><td>' . $b . '</td><td>' . $c . '</td><td>' . $d . '</td>
	<td>' . $e . '</td><td>' . $f . '</td><td>' . $g . '</td><td>' . $h . '</td><td>' . $i . '</td>
	<td>' . $j . '</td><td>' . $k . '</td><td>' . $l . '</td><td>' . $m . '</td><td>' . $n . '</td>
	<td>' . $o . '</td></tr>';

$planificationTable .=  '</table>';
print $planificationTable;

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.