Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing variables

Status
Not open for further replies.

jbeetz1

Programmer
Nov 28, 2000
60
0
0
US
I'm new at PHP. What I'm trying to accomplish is creating a form that has many radio buttons, drop down lists and text fields and submitting the data to a MYSQL database without using a submit button on the form.

Here is one of the objects on the page:
<p align="left">Draft Type:&nbsp; <input type="radio"CHECKED value=A onclick="update_db('draft_type',this.value)"
name=draft_type >&nbsp;Annual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=radio value=W onclick="update_db('draft_type',this.value)" name=draft_type >&nbsp;Weekly</p>

I created a function on the page that does nothing but updates the database passing in the value as shown below.

function update_db(field,test12)
{
document.getElementById("answer").value=test12;

<?
$league_id = 101;


$sql_usr_details = "INSERT INTO league_options(league_id,season_start,season_end)
values('$league_id','2009-10-7','2009-12-20')";

mysql_query($sql_usr_details);

?>
if (field=='draft_type')
{
<?

$sql_update = "update league_options set draft_type = '$draft_type' where league_id = '$league_id'";
mysql_query($sql_update)
?>
}
}

The function does execute but I cannot figure out how to get the value being passed to "test12" into the SQL update command and the $draft_type variable.

Thanks in advance
Jerry
 
Hi Jerry,
You either need to submit the form or use AJAX. You can have the form be submitted to itself, causing the page to reload, without using a submit button. I'm guessing you'll want an AJAX solution, pages the auto reload on a value change bug me. Either way its a bit of php and javascript. The php end will be something like
Code:
$drafe_type = $_REQUEST['answer'];
But one way or another, you'll need to pass the answer variable to a php script to process it.

-----------------------------------------
I cannot be bought. Find leasing information at
 
Basically PHP cannot interact with Js that way. PHP is a server side language which means it runs on the server not he client's browse.

For PHP to actually start working and update the database the form must be submitted to the server so that PHP may run.

What yu want to accomplish can be done through AJAX by hAving it call the PHP script and passing to it the form value to update the database.

Embedding PHP in a JS function can never work, because the run in 2 different environments. By the time the Javascript function is called, PHP is no longer running.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top