#1
  1. No Profile Picture
    Callin' PHP Docs !
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    74
    Rep Power
    12

    Question PHP and Javascript


    Hi am wondering if you guys can help me out. I am not sure if theres an equiv php coding or javascript i can use.

    Baiscly i would like to get a pop up screen that would ask the users if they want to go ahead with the modification. If okay then "SET UPDATE SQL TABLE" if Cancel or no then return to the same page.

    I tried this logic but it didnt work:

    if($HTTP_POST_VARS['Submit'] == 'Modify') {

    x=window.confirm(\"Modifications for $mresname will be saved. Click Ok to continue Cancle to return. \")\n";
    if (x);
    .......

    else

    redirect...

    The issue is i do not want the PHPSELF to be activated as in the modification goes through when the person presses "cancel". I want it to stop as if the submit button hasnt been pressed.

    Any thoughts ??

    Cheers

    Sara
  2. #2
  3. No Profile Picture
    Callin' PHP Docs !
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    74
    Rep Power
    12

    Maybe confusing...


    I read what i wrote i thought it might be confusing. Let me rephrase the last part.

    Since its PHPSELF if you press cancel and loads the page as in it redirects it might read it as if someone made a submission. So in addition to my first question how do i avoid this within the code.

    Thank u all for the help ! Love this forum

    Sara
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    16
    Since you're wanting a pop-up confirmation dialog, I'd say you would want to use javascript and put an onSubmit() handler in the <form> tag.
  6. #4
  7. No Profile Picture
    Callin' PHP Docs !
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    74
    Rep Power
    12

    Unhappy Now iam confused !


    Can someone help me out with the coding ? How can i combine PHPSELF and the onSubmit () function ?

    I am not sure if they can work together.

    Thanks again,

    Sara
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    16
    SaraPHP,

    I just threw this together so it isn't perfect but it should give you some idea of how to control the form submission using client side scripting in combination with php.

    PHP Code:
    <?php
    $thispage 
    $_SERVER['PHP_SELF'];
    if(!empty(
    $_POST['txtName'])){
        
    // Process form data
        
    $name $_POST['txtName'];
        print 
    "$name - You have submitted this form";
        }
    ?>
    <html>
    <head>
    <title>Doing Javascript    from PHP</title>
    <script language="javascript">
    function    confirm_submit(){
        if(confirm("Are you sure you want to submit these changes?"))
        {
             return true;
            }
            else
            {
                 return false;
                }
         }
    </script>
    </head>
    <body>
    <form name='frmSignUp' method='POST' action="<?php echo $thispage?>" onSubmit="return confirm_submit();">
    <input type='text' name='txtName'>
    <input type='submit' value='submit'>
    </form>
  10. #6
  11. No Profile Picture
    Callin' PHP Docs !
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    74
    Rep Power
    12

    Didnt work...


    Thanks for the effort ...but it didnt work with me. I think its gots to do with the mix of PHPSELF and use of Form. I tried placing ur code here and there the modifications didnt go through or went through repeatedly once the page refreshed.

    Can you place have a look at the code and tell me where i went wrong ? Cheers folks !

    <?

    // connection

    $sql = "SELECT * FROM $table_name WHERE rid = \"$rid\"
    ";

    $result = mysql_query($sql)
    or die ("Can't execute query.");

    while ($row = mysql_fetch_array($result)) {

    $var = $row['fetched'];
    }

    $mod = "

    //html block with textbox called var

    <SCRIPT LANGUAGE=JAVASCRIPT>

    function verify(){
    if(document.forms[0].var.value==\"\"){
    alert(\"Please enter var.\");
    return false;
    }

    return true;

    }
    </SCRIPT>

    <form name=\"form\" method=\"Post\" onSubmit=\"return verify()\" action=\"$PHPSELF\" >
    ";

    echo $mod;

    -------------------> Here is where i should get a confirmation window if yes then the following. If not then just return to this page without modifying.
    --------------------------------->

    //Sql statement and modification entry
    $date = date("d-m-Y");
    $time = date("H:i:s");

    $sql = "UPDATE tablename WHERE rid = \"$rid\" SET (fetched)
    VALUES ('$var')";

    $result = mysql_query($sql)
    or die ("Can't modify the database.");


    //pop to show user modifcation has been completed
    <SCRIPT LANGUAGE=JAVASCRIPT>
    window.alert(\"Datanase Successfully Modified.\")
    </SCRIPT>

    }

    // Should stop here but it reloads the page and keeps modifying.
    ?>
  12. #7
  13. No Profile Picture
    Callin' PHP Docs !
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    74
    Rep Power
    12

    Unhappy Still working on it !....


    Still working on folks...and i get it right !!!! Where did i go wrong ?
  14. #8
  15. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    16
    Hi Sara,

    First of all please format your code with vB Code for easier reading and detection of some basic errors... also please make sure your code if written in a neat way (use tabs, explaining comments and enters to group pieces of code)... I played a bit with your code:
    PHP Code:
    <?

    // connection

    $sql "SELECT * FROM $table_name WHERE rid = \"$rid\""
    $result mysql_query($sql) or die ("Can't execute query."); 
    while (
    $row mysql_fetch_array($result)) {
        
    $var $row['fetched'];
    // Mirax: why use a while loop if you get one result anyway?

    $mod "
        //html block with textbox called var --> Mirax: put this outside the 
    $mod-var?
        <SCRIPT LANGUAGE=JAVASCRIPT>
        function verify(){
            if(document.forms[0].var.value==\"\"){
                alert(\"Please enter var.\");
                return false;
            }
            return true;
        }
        </SCRIPT>
        <form name=\"form\" method=\"Post\" onSubmit=\"return verify()\" action=\"
    $PHPSELF\" >
    "
    ;
    echo 
    $mod;
    /*
        Mirax: where is the rest of the form??
    */

    // Here is where i should get a confirmation window if yes then the following.
    // If not then just return to this page without modifying.

    //Sql statement and modification entry
    $date date("d-m-Y");
    $time date("H:i:s");
    /*
        Mirax: what are you doing with this?!?
    */

    /*
        Mirax: you should set the update part within a 'if ( isset( $_POST['submit'] ) ) {' or something
        so it will only be executed when the submit-button is pressed
    */

    $sql "UPDATE tablename WHERE rid = \"$rid\" SET (fetched) VALUES ('$var')";
        
    // Mirax: why not: SET fetched='$var' ?
    $result mysql_query($sql) or die ("Can't modify the database."); 

    /*
        Mirax: missing ?> here
    */

    //pop to show user modifcation has been completed
    <SCRIPT LANGUAGE=JAVASCRIPT>
        
    window.alert(\"Datanase Successfully Modified.\")
    </SCRIPT>

    }

    // Should stop here but it reloads the page and keeps modifying.
    // Mirax: reloads?
    ?>
    I didn't test it but it seems to me to be a bit sloppy/unclear if I might say so.. it also seems you left pieces out... anyway, the code micros_bytes provided worked for me so I suggest you start simple with your own script and start with a page which doesn't use javascript yet... just a simple page which has a form which leads to the update-section which outputs the query instead of executing it... then add the javascript piece by piece and if it all works execute the query as well...
    There are 10 types of people in this world - those who understand binary and those who don't...

    PHP | MySQL | DevShed Forum Search | Google Search
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    16
    I can't tell by looking at your code but are you trying to reference the value of a PHP variable from within the Javascript function?

    What element in your form are you referring to in the javascript
    document.forms[0].var.value ? Or, are you trying to reference the $var variable from PHP?

    You should post your HTML form along with the PHP.
  18. #10
  19. _/__/___/_____/________/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    nycnyus
    Posts
    27
    Rep Power
    0

    alt attempt


    -- i got a bit motivated, and:

    PHP Code:
    <?php
    if($isSub==1){
        if(
    $jsConf==1||$tpConf==1){
            
    // sql insert junk here
            
    $anymsg "Beautiful. Cake \"$cake\" made.<br><br>Make more cake?";
        }else{
            
    $anymsg "You have flaky or no JS - please resubmit this form to confirm making cake \"$cake\".";
            
    $twopass 1;
        }
    }else{
        
    $anymsg "Please fill out the form.";
    }
    ?>
    <html>
    <head>
        <script language="javascript"><!--
            // pretty liberally bitten from code in phpma
            function confForm(formName, formMsg){
                var isConf = confirm(formMsg);
                if(isConf){
                    document.forms[formName].jsConf.value = 1;
                }else{
                    isConf = false;
                }
                return isConf;
            }
        //-->
        </script>
    </head>
    <body>
    <?php echo $anymsg?><br>
    <form name="cakemixform" action="?" method="post" onsubmit="return confForm(this.name, 'This will make cake.\nAre you absolutely positive you want to do this?');">
    <input type="hidden" name="isSub" value="1">
    <input type="hidden" name="jsConf" value="0">
    <?php if($twopass){ ?><input type="hidden" name="tpConf" value="1"><?php ?>
    <input type="text" size="10" maxlength="10" name="cake" value="<?php echo $cake?>"><br>
    <input type="submit">
    </form>
    </body>
    </html>
    -- this works
  20. #11
  21. No Profile Picture
    Callin' PHP Docs !
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    74
    Rep Power
    12

    Thank u folks !


    Hey peeps !

    Thanks ..Bzzp for the code i will try it out and see what happens.

    micros_bytes.....

    Just wanted to explain that i didnt attach the form because it was a long html. The variable $var indicates that its a textbox called var.

    I am just using the javascript to make sure that the entries are entered and to confirm wither to go ahead with the modification

    mirax...

    The html coding i removed it coz i was too long. But baiscly imagine that $var is a textbox called var. The date and time i am using them to record the modification time. SQL statement is not shown because i know it works. Finally by reloads i mean the page doesnt do anything no pop up just a normal refresh of the page.

    Hope that makes it clear guys.

    Thanks again

IMN logo majestic logo threadwatch logo seochat tools logo