/    Sign up×
Community /Pin to ProfileBookmark

Adding/Passing a Variable

Hello all,

I have the below function that opens a record based on the “id” returned from my query. What I want to do is pass another variable “request_type” through the javascript and then run a query based on the two passed variables (id and request_type)

I have it passing in the ID fine but I can’t seem to get the request_type to pass through.

Here is the code for the request_type:

[code=html]<input type=”hidden” name=”request_type” value=”C”>[/code]

And here is my function:

[CODE]
function editRecord(id) {
var LeftPosition = (screen.width – 800) / 2;
var TopPosition = (screen.height – 600) / 2;
var link;
link = ‘edit.php?id=’ + id;
MyWin = window.open(link,”EditRecord”,”toolbar=no,scrollbars=yes,resizable=no,top=”+TopPosition+”,left=”+LeftPosition+”,location=no,directories=no,status=no,menubar=no,width=760,height=550″);
MyWin.focus();
}
[/CODE]

How would I get the value from request_type into my function and have it pass to the next page for use in my query?

Thanks for any help.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 01.2007 — I don't really see enough HTML to give you the code. However, you could do something like this:
[code=html]<input type="hidden" name="request_type" id="txt_request_type" value="C">[/code]
Then call the editRecord() function like this:
editRecord(document.getElementById('txt_request_type').value);
Copy linkTweet thisAlerts:
@mrsquashauthorMay 02.2007 — I'm not sure if we are thinking of the same functionality. Or maybe I am just not understanding.

I have a query:

[code=php]<?php
***connection information omitted***
// Connect to the database
$connection = mssql_pconnect($host, $user, $pass) or die ('server connection failed');
$database = mssql_select_db("$database", $connection) or die ('DB selection failed');
// Query the table and load all of the records into an array.
$sql_payments = "SELECT * FROM payment_request
WHERE payment_request.status_code = 'P'
AND payment_request.credit_card_id = '$current_id'";
//print_r ($sql_child);
$result_payments = mssql_query($sql_payments) or die(mssql_error());
//print_r ($result_child);
?>[/code]


The results of the query are put in a loop:

[code=php]<?php
if(!empty($result_payments)) {
while ($row_payments = mssql_fetch_array($result_payments)) {
$id = $row_payments[id'];
$case_number = $row_payments['case_number'];
$payment_amount = $row_payments['payment_amount'];
$total += $row_payments['payment_amount'];
?>[/code]

That returns rows:

[code=html]<tr>
<td width="21">ID:</td>
<td width="88"><?php echo "<a href='javascript:editRecord($id)'>$id</a>" ?></td>
<td width="98">Case Number:</td>
<td width="252"><?php echo $case_number; ?></td>
<td width="116"><div align="right">Payment Amount:</div></td>
<td width="105"><div align="right">$<?php echo number_format($payment_amount, 2); ?></div></td>
<input type="hidden" name="request_type" value="C">
</tr>[/code]


The first field of each returned row is the ID, which when clicked calls a Javascript function:

[CODE]function editRecord(id) {
var LeftPosition = (screen.width - 800) / 2;
var TopPosition = (screen.height - 600) / 2;
var link;
link = 'edit.php?id=' + id;
MyWin = window.open(link,"EditRecord","toolbar=no,scrollbars=yes,resizable=no,top="+TopPosition+",left="+LeftPosition+",location=no,directories=no,status=no,menubar=no,width=760,height=550");
MyWin.focus();
}[/CODE]


The Javascript function opens up the record that corresponds to the ID passed.

This all works fine. What I am having trouble with is trying to pass the value of the hidden field (request_type) through Javascript so that when the record opens up, I can use the passed variable in a query on the new page.
Copy linkTweet thisAlerts:
@toicontienMay 02.2007 — Ah, ok. There are a couple ways of going about this. From the window that opened up, you can access the request_type value from the hidden field:

var request_type = window.opener.document.getElementById('txt_request_type').value

Or, you can download a short script I wrote that allows you to access the variable-value pairs in the URL just like you can in PHP: http://javascript.internet.com/page-details/get-web-address-reader.html (Note: javascript.internet.com was down at the time I posted this reply).
Copy linkTweet thisAlerts:
@mrsquashauthorMay 02.2007 — I can access the request_type value from the hidden field even though my function does not perfrom "myform.submit" and with the request_type not being put into a variable in the edit function ???

I will check your link when the page is active again.
Copy linkTweet thisAlerts:
@toicontienMay 02.2007 — In the window that opens up, JavaScript can access the window object of the window that opened it up. Example:

  • 1. Window A opens up a new window called B.


  • 2. In window B, a JavaScript can use window.opener to access the window object of window A.
  • Copy linkTweet thisAlerts:
    @mrsquashauthorMay 02.2007 — Ah, I understand now. Thank you for explaining that to me.
    Copy linkTweet thisAlerts:
    @mrsquashauthorMay 02.2007 — Hmmmm, it still doesn't seem to work.

    Page B I put in the following javascript:

    [CODE]var request_type = window.opener.document.getElementById('txt_request_type').value
    document.Submit.request_type.value = request_type[/CODE]


    This should take the value from Page A and populate it to Page B's request_type field, right?

    [code=html]<form name="Submit" action="save.php" method="post" enctype="multipart/form-data">
    <input type="text" name="request_type" value="" id="txt_request_type">[/code]
    Copy linkTweet thisAlerts:
    @toicontienMay 02.2007 — In page A, the page that opens the new window, the hidden field called "request_type" needs to have an id of "txt_request_type" for this to work.
    Copy linkTweet thisAlerts:
    @mrsquashauthorMay 02.2007 — It does.

    Page A:

    [code=html]<tr>
    <td width="21">ID:</td>
    <td width="88"><?php echo "<a href='javascript:editRecord($id)'>$id</a>" ?></td>
    <td width="98">Case Number:</td>
    <td width="252"><?php echo $case_number; ?></td>
    <td width="116"><div align="right">Payment Amount:</div></td>
    <td width="105"><div align="right">$<?php echo number_format($payment_amount, 2); ?></div></td>
    <input type="hidden" name="request_type" id="txt_request_type" value="C">
    </tr>[/code]


    Calls the below javascript:

    [CODE]function editRecord(id) {
    var LeftPosition = (screen.width - 800) / 2;
    var TopPosition = (screen.height - 600) / 2;
    var link;
    link = 'edit.php?id=' + id;
    MyWin = window.open(link,"EditRecord","toolbar=no,scrollbars=yes,resizable=no,top="+TopPosition+",left="+LeftPosition+",location=no,directories=no,status=no,menubar=no,width=760,height=550");
    MyWin.focus();
    }[/CODE]


    Page B:

    This should place the value into a variable and assign the form value.
    [CODE]var request_type = window.opener.document.getElementById('txt_request_type').value;
    document.Submit.request_type.value = request_type;[/CODE]


    This is the form info and the input field that should be populated, but it returns blank.
    [code=html]<form name="Submit" action="save.php" method="post" enctype="multipart/form-data">
    <input type="text" name="request_type" value="" id="txt_request_type">[/code]
    Copy linkTweet thisAlerts:
    @mrsquashauthorMay 03.2007 — bump to the top?
    Copy linkTweet thisAlerts:
    @toicontienMay 03.2007 — I'm not sure. I'd have to see the two pages in action. Are you getting any JavaScript errors? Try alerting window.opener.document in the script on page B to see if the document object that [I]should[/I] reference is indeed an object, and not undefined. You could also try this format for populating the form value on page B:

    document.forms['Submit'].elements['request_type'].value = request_type;

    Better yet, give that form on page B an Id so you can do this:
    document.getElementById('myFormId').elements['request_type'].value = request_type;
    ×

    Success!

    Help @mrsquash spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...