shuffle Posted June 16, 2011 Share Posted June 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/ Share on other sites More sharing options...
Jeffro Posted June 16, 2011 Share Posted June 16, 2011 oops.. i retract my answer as it didn't answer the "viewstate" dilemna. Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1230556 Share on other sites More sharing options...
shuffle Posted June 20, 2011 Author Share Posted June 20, 2011 I'm still looking for a solution. Anyone got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1232202 Share on other sites More sharing options...
trq Posted June 20, 2011 Share Posted June 20, 2011 You could always store a copy of the $_POST array into the users $_SESSION array (or even just the values you want) when the form has been submitted then easily restore them from there. Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1232204 Share on other sites More sharing options...
shuffle Posted June 20, 2011 Author Share Posted June 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1232263 Share on other sites More sharing options...
KevinM1 Posted June 20, 2011 Share Posted June 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1232274 Share on other sites More sharing options...
shuffle Posted June 21, 2011 Author Share Posted June 21, 2011 Ok I'll try to do that. I'll have to learn more AJAX as I only have a rough understanding of it. Thanks for you help. Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1232819 Share on other sites More sharing options...
shuffle Posted June 22, 2011 Author Share Posted June 22, 2011 Well, I'm still not getting much further with this as i thought. If its not to much trouble can some one post some basic example code for doing this? Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1233379 Share on other sites More sharing options...
shuffle Posted June 23, 2011 Author Share Posted June 23, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1233935 Share on other sites More sharing options...
shuffle Posted June 28, 2011 Author Share Posted June 28, 2011 I still can't find a solution for this. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/239552-how-to-create-aspxs-postback-effect-in-php/#findComment-1235996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.