Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/242886-populate-hidden-field-from-php-database/
Share on other sites

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.

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>

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.

 

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.