dfalkowitz Posted July 26, 2011 Share Posted July 26, 2011 Is there a way to call a PHP database within a *.html page and populate the answer to a hidden field? For example: SQL DB has 2 fields ID and SEGMENT, ID SEGMENT 1 A 2 B 3 C 4 D I have a form: <form action="Untitled-2.php" method="post" name="form1" id="form"> <input type="radio" name="ID" value="4" /> 4</label> <label> <input type="radio" name="ID" value="3" /> 3</label> <label> <input type="radio" name="ID" value="2" /> 2</label> <label> <input type="radio" name="ID" value="1" /> 1</label> <input name="segment" id="segment" type="hidden" /> I want to be able to have a button onclick() go out the the php server collect which Segment it is and populate the hidden text field "segment" then submit all on the same HTML page? Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/ Share on other sites More sharing options...
sunfighter Posted July 27, 2011 Share Posted July 27, 2011 Your example makes no sense. When you submit the form the radio button values will go to Untitled-2.php and you can look up the SEGMENT value then. Why go to the extra trouble to get it before hand and hide it? You may have a better real example where this is needed, but this is not the one. And Yes you could do it with AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/#findComment-1248104 Share on other sites More sharing options...
dfalkowitz Posted July 27, 2011 Author Share Posted July 27, 2011 Thanks for the response! Yes, I know I can show on the php page but currently for this project I need it to populate the hidden field on current page before it goes to new page. Do you have a sample of how it can be done in AJAX? Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/#findComment-1248107 Share on other sites More sharing options...
sunfighter Posted July 28, 2011 Share Posted July 28, 2011 You have to check this out yourself and write your own ajax scripts. EXPLANATION: Each radio button must call ajax and send unique info to it. You have to place the return text from ajax into the hidden field value. You should submit the form through javascript and check that the hidden field has been changed and you might have to add a delay here in case the ajax is slow in coming. This is NOT final coe but for show and learning only: <!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" xml:lang="en" lang="en"> <head> <script type="text/javascript"> function ButtonInfo(butn_id) { switch(butn_id) { case 'num1': document.getElementById('segment').value = makeRequest('num1'); break; case 'num2': document.getElementById('segment').value = makeRequest('num2'); break; case 'num3': document.getElementById('segment').value = makeRequest('num3'); break; case 'num4': document.getElementById('segment').value = makeRequest('num4'); break; } } function makeRequest(from_message) // get your info and write to hidden field via Ajax { //write your ajax code here // use RETURN for the returned ajax text return('Its all for the greater good'); } function submitform(){ document.getElementById('For_info').innerHTML = document.getElementById('segment').value; // Just make sure the hidden has correct info } </script> </head> <body> <form action="Untitled-2.php" method="post" id="form"> <input type="radio" name="num" value="num4" onchange="ButtonInfo('num4');" />4 <input type="radio" name="num" value="num3" onchange="ButtonInfo('num3');" />3 <input type="radio" name="num" value="num2" onchange="ButtonInfo('num2');" />2 <input type="radio" name="num" value="num1" onchange="ButtonInfo('num1');" />1 <input name="segment" id="segment" type="hidden" value="Some stuff" /> <input type="button" value="Submit" name="my_form_button" onclick="submitform();" /> </form> <div id="For_info"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/#findComment-1248701 Share on other sites More sharing options...
dfalkowitz Posted July 29, 2011 Author Share Posted July 29, 2011 Thanks sunfighter for response! What I was trying to do was to populate the hidden text field below the radio button question. What would the it look like for just updating the hidden field? Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/#findComment-1248970 Share on other sites More sharing options...
sunfighter Posted July 29, 2011 Share Posted July 29, 2011 I did update the hidden field and then displayed what is in the hidden field. Updated the hidden field with these => document.getElementById('segment').value = makeRequest('num1'); And displayed the hidden field with this => document.getElementById('For_info').innerHTML = document.getElementById('segment').value; The hidden field here => <input name="segment" id="segment" type="hidden" value="Some stuff" /> has "Some stuff" as it's value when we start and that should change when you select a radio box. What it changes to is in this example is 'Its all for the greater good', taken form this => return('Its all for the greater good'); but your ajax will be different. Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/#findComment-1249115 Share on other sites More sharing options...
dfalkowitz Posted July 29, 2011 Author Share Posted July 29, 2011 ok thanks, I will try... Quote Link to comment https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/#findComment-1249122 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.