SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a couple questions regarding how php works with javascript. If there are already threads and/or links that answers these questions, you can just link me to them instead of going through the whole process of explaining it to me.

    Basically, can/how does php and javascript pass variables back and forth to each other.

    Example one:

    If I want to delete something from a database, I want to display a javascript confirmation message if this is really what they want to do or not. The confirmation is in javascript but everything else is in PHP. I would need to test if the confirmation is true or not and depending on that preform different php functions.

    Example two:

    On one of my pages I have a form that is filled out for different client problems when they call in. I want to enable it so after a client name is filled in and I leave the form "name" field, it will preform a database query for that name and return the results back to the form filling in the rest of the information for that client; such as address, phone, ext. I know how to pass form variables from one page to another with javascript, and I know how to setup the PHP query. I just can't figure out how to link the two languages so they work together. I've read Kevin's "No-Refresh Links" article and have tried this type of technique to do the query, but I don't know how to then pass the variables back to the parent page's form to fill in the rest of the information.

    There are other situations where I use javascript and PHP hand in hand. I usually do a print("<script>bla bla bla</script>"); type of deal and it works pretty good, but if I have to pass variables or information from one to the other, I run into problems.

    Any help and/or resources anyone can provide would be greatly appreciated.
    Last edited by HotDog; Feb 26, 2001 at 14:03.
    Joe Eliason
    Just a dog learnin' PHP from cat.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Joe here is the first one:

    Javascript, I normally pass a title for the record to use in the confirm statement, but this optional, if you don't want it just remove it from the function:

    Code:
    function confirmit(titler, delid) {
    		var agree = false;
    		agree = confirm('Are you sure you want to permanently delete ' + titler + ' from the database?');
    		if (agree) {
    			location.href = '<? print $PHP_SELF; ?>?delid=' + delid;
    			}
    		else {
    			return;
    			}
    	}
    And how to call it in PHP

    PHP Code:
    printf("<a href=\"javascript:confirmit('%s', '%s'); \">delete item</a>"$title$id); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anyway to pass a javaScript value to a PHP variable? I know you can use a hidden form field, but that won't work in many situations. Can you eval() a javascript string in PHP to caputre it's value to a PHP variable. I've tried this b4, but was unsuccessful, but I don't know if I just wasn't doing it right or if it just isn't possible.

    If you need a specific example of when or where I need to do this I'll let you know.
    Joe Eliason
    Just a dog learnin' PHP from cat.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Joe, I am not really following what you need to do. Remember that js is client side and PHP is server side. In order to get a var from the client to the server you either need to submit a form or click a link.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry I've been so vague on what it is I'm trying to accomplish. I guess I've been trying to solve a general concept problem instead of a specific problem. Basically this is what I'm trying to do. I have a form that is filled out to keep track of calls from different clients. The form starts out by entering the client info and contact person for that client. This information is stored in their respective tables. The rest of the form is the trouble ticket for that client. What I would like to do is design the form so that one could just type in the client's name or the client's group number and the rest of the client information (address, phone, etc.) will automatically load in.

    I can't capture the information when the page is first loaded since the client is unknown, unless I have a preform page that asks this information before passing it on the the main ticket form. But since I'm trying to keep everything on just one form, I've been trying to come up with a way that I could generate the information.

    I realize that javaScript is client side and PHP is server side, so my idea was to pass the information to another page, have PHP do it's thing, then have javaScript pass it back to the parent window inputting the information into the form.

    To do this I thought of 2 possible solutions. At first I thought of passing the information to a page such as described in Kevin's "No-refresh links" article where the page is called but nothing is sent to the browser. All the work is done by the server. But this doesn't work since I need to pass information back and update the original form using javaScript. So what I then came up with was to make a simple javascript that opens up a popup window once I enter either the client name or group number (onblur='open(bla bla bla)' type of deal). Then in the popup window I would query the database for the information I needed, and have a javascript to send the information back to the original form updating the corresponding fields.

    Now this is where my whole problem and question is. In order to preform the query I need the client name or group number so I can preform the query. How do I pass this value from one window to the next without using a form submit? Since I'm not submitting the form the value isn't in the url. I know that in PHP I can assign a string to a variable by saying $str="this is a string";. My idea then was to capture the parent form field's value using javascript, then passing that string value to a PHP variable so I could preform a query. My idea was something like $var = eval("<script>opener.mainForm.clientName.value</scritp>"); or to capture the form field's value as a javaScript variable and then passing the javaScript variable to a PHP variable, as a string. If I could do this, I could now query the database by saying $query = mysql_query("SELECT * FROM clients WHERE name = '$var');
    $result = mysql_fetch_row($query); --- and so forth
    . I could then very easly echo a javaScript passing the PHP variable to the parient form. I thought I could evaluate the javascript value into a string using this method, but it doesn't seem to work. So my question is...Is it possible to to give a PHP variable a string value evaluating a javascript value?? If this is possible, it would not only solve this problem, but others like it.

    While writing this I've come up with another idea. Could I split my form into two, the first one being the client information, and the second being the rest of the call ticket? Then could I open a popup window using a simular method as before but also using a submit() function as well to then pass the information to the popup window. For example...
    Code:
    <form method=post action="javascript: open('bla bal bla')"> ...
    <input name="clientName" type="text" onblur="submit()">
    I hope this gives a better idea of what I'm trying to do and what my very poorly asked question is. Sorry if this is a stupid question, I just haven't worked with PHP enough to know what all it can and can't do.

    If what I'm trying to do can't be done and you have any other solutions/ideas/work-arounds to my problem, I'd be very thankful to hear them.

    p.s. freddy, I've found the answer to the MS ACCESS LIMIT problem from a couple weeks ago if you'd like to know.
    Last edited by HotDog; Feb 27, 2001 at 01:04.
    Joe Eliason
    Just a dog learnin' PHP from cat.

  6. #6
    SitePoint Addict
    Join Date
    Nov 2000
    Location
    London, UK
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you think that you could have a form that, when the name is entered, writes the rest of the form with whatever html is returned by a php page to whom you pass the name? This would mean that in the beginning, there wouldn't be anything there, just the client name field. When its entered, the rest of the form appears:
    1) filled in if the client exists
    2) empty if they don't
    I don't know JavaScript too well, but I know general concepts, and it might look something like this (pseudo code):

    onblur(namefield);
    document.write(html from yourfile.php?name=form.namefield);


    There should be a way to do this easily enough, although I'm not exactly sure.
    The other way would probably be to have javascript get info from the querystring and fill it into the form. Then, the onBlur could redirect to the PHP page with the name, who could header back to the form page with the info.

    If it sounds odd, ask and I shall explain again.

    ... what's the world coming to?

  7. #7
    SitePoint Zealot HotDog's Avatar
    Join Date
    May 2000
    Location
    Salt Lake City, UT
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a good way of accomplishing what I'm trying to do. I just setup 2 different variations of the form seperated in an IF/THEN and once the values are entered I just submit the form page to itself and fill in the values to the other form. I then setup a simple javascript to look to see what value was entered (the name or the number) and submit a PHP variable to then query the record by WHERE name or WHERE number.

    <?php
    //Hook up to the database and include common variables
    include("common.inc");

    //Display javaScript
    //$script = print("<script language=javaScript>\n"
    . "function validString(str)\n"
    . "{\n"
    . "if (str.length != 0) \n"
    . "return true\n"
    . "else\n"
    . "return false\n"
    . "}\n"
    . "function checkform(form) \n"
    . "{\n"
    . "if (validString(form.clientName.value) != false) \n"
    . "{\n"
    . "form.submit()\n"
    . "return false\n"
    . "}\n"
    . "if (validString(form.groupNmbr.value) != false) \n"
    . "{\n"
    . "form.submit()\n"
    . "return false\n"
    . "}\n"
    . "}\n"
    . "</script>");

    //Load default page header information
    include("header.inc");

    if ("Submit Form"!=$enterForm) :
    if (!isset($clientInfoSubmit)) :
    ?>
    <!--Enter Body Context-->
    <form method=get action="<? echo $PHP_SELF; ?>" name="clientInfo">
    <input type=hidden name=clientInfoSubmit value=1>
    <TABLE border=1 frame=box rules=none align=left width=600>
    <tr>
    <td>
    ...
    <table border="0" cellspacing=3 cellpadding=4>
    <tr>
    <td><p class=right><b>Client Name/b></p></td>
    <td><input name="clientName" size="35" value="" onblur="return checkform(this.form)"></td>
    <td><p class=right><b>Group # /b></p></td>
    <td><input name="groupNmbr" size="10" onblur="return checkform(this.form)"></td>
    </tr>
    ...
    </table>
    </tr>
    </form>
    <script language="javaScript">
    clientInfo.clientName.focus();
    </script>
    <?php
    else:
    if ($clientName!=""){
    $query = mysql_query("SELECT * FROM gaclients WHERE name='$clientName' ");
    }
    elseif ($groupNmbr!=""){
    $query = mysql_query("SELECT * FROM gaclients WHERE groupNmbr='$groupNmbr' ");
    }
    else die("error");
    $result = mysql_fetch_array($query) or die("Client doesn't exist");
    $clientName = $result["name"];
    $groupNmbr = $result["groupNmbr"];
    $clientPhone = $result["phone"];
    $clientFax = $result["fax"];
    ?>
    <form method=get action="<? echo $PHP_SELF; ?>" name="mainForm">
    <input type=hidden value=clientInfoSubmit>
    <TABLE border=1 frame=box rules=none align=left width=600>
    <tr>
    <td>
    <table border="0" cellspacing=3 cellpadding=4>
    <tr>
    <td><p class=right><b>Client Name/b></p></td>
    <td><input name="clientName" size="35" value="<? echo $clientName ; ?>" onfocus="document.mainForm.contactName.focus()"></td>
    <td><p class=right><b>Group # /b></p></td>
    <td><input name="groupNmbr" size="10" value="<? echo $groupNmbr ; ?>" onfocus="document.mainForm.contactName.focus()"></td>
    </tr>
    <tr>
    <td><p class=right><b>Phone/b></p></td>
    <td><input name="clientPhone" size="15" value="<? echo $clientPhone ; ?>" onfocus="document.mainForm.contactName.focus()"></td>
    <td><p class=right><b>Fax/b></p></td>
    <td><input name="clientFax" size="15" value="<? echo $clientFax ; ?>" onfocus="document.mainForm.contactName.focus()"></td>
    </tr>
    ...
    </table>
    </td>
    </tr>
    </form>
    <script language="javaScript">
    mainForm.contactName.focus();
    </script>
    <?php
    endif;
    include("footer.inc");

    else :
    $sql = "INSERT INTO gatickets (bla, bla, bla)"
    . "VALUES ('$bla', '$bla', '$bla',')";
    $result = mysql_query($sql);

    ...

    endif;

    //display page footer information
    include("footer.inc");
    ?>

    This is stripped down, but I hope you can follow it. But I still have the question of is it possible to pass a javascript string value to a PHP variable.
    Last edited by HotDog; Feb 27, 2001 at 17:22.
    Joe Eliason
    Just a dog learnin' PHP from cat.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •