SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast SlayMan's Avatar
    Join Date
    Oct 2004
    Location
    Germany/ Hamburg
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using Javascript to check whether a value is in mySQL or not inside of a php page

    I have a php script which updates a tablerow from mySQL with posted variables, so far so good. I have a value in the post array which belongs to another table.
    I want to runtime check it (thatfor Javascript) if that variable is already in the db. if its not then i wanna to add it
    i do want to use Javascript and have a popupwindow which asks if i wanna to add the value or not.
    heres the piece of php-code:
    PHP Code:
    if (isset($_POST['edit']))
    {
        
    reset($_POST);
        
    $sql="UPDATE benutzer SET ";
        for (
    $i=0$icount($_POST)-1$i++)
        {
            if ((
    key($_POST)!='UID') && (current($_POST)!='') && ((key($_POST)!='client')))
            {
                
    $sql.=key($_POST)."='".current($_POST)."', ";
            }
            
    next($_POST);
        }
        
    reset($_POST);
        
    $sql.="WHERE UID='".$_POST['UID']."'";
        
    $error="Update des Benutzers fehlgeschlagen: ".mysql_error();
        
    $result=@mysql_query($sql) or die ($error);
        unset(
    $sql);
        unset(
    $result);
        

    i know it looks kinda weird but it works good and it looks good to me.
    now what i want to check:
    PHP Code:
    $result=@mysql_query(SELECT CID FROM kunden WHERE client='".$_POST['client']."')
    if (
    mysql_num_rows($result)==0)
    {
    /* HERE I WANT TO ASK JAVASCRIPT IF TO ADD NEW CLIENT AND DO IT AFTERWARDS with all the rest of the submitted values*/

    SlayMan,
    Your friendly neighbourhood trainee and webmaster
    go, see for yourself @ http://www.alpupp.de

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we asume that u use for loop on mysql_num_rows($ret)
    PHP Code:
    if(empty($row[$i])) {
    //update

    cheers

  3. #3
    SitePoint Enthusiast SlayMan's Avatar
    Join Date
    Oct 2004
    Location
    Germany/ Hamburg
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am sorry i do not understand what u r talking about. Could u please explain?
    SlayMan,
    Your friendly neighbourhood trainee and webmaster
    go, see for yourself @ http://www.alpupp.de

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u want to check if the row have an value inside and then u want to avoid this field if the value is inside?
    if not the field have to be updated?

  5. #5
    SitePoint Enthusiast SlayMan's Avatar
    Join Date
    Oct 2004
    Location
    Germany/ Hamburg
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i want to check if the content of $_POST['client'] is already existing in the table of kunden
    but in the runtime.
    if it does exist nothing particular will happen, if it does not exist i want to ask the customer if he is willing to add $_POST['client'] to the table.
    SlayMan,
    Your friendly neighbourhood trainee and webmaster
    go, see for yourself @ http://www.alpupp.de

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    // select from database
    for(reset($i); $i <= mysql_num_rows($ret); ++$i) {
    $row = mysql_fetch_array($ret);
    if(empty($row[$i])) {
    ?><script>
    if(confirm("Do you want update <?php echo $row[$i]; ?>?")) update();
    function update() {
    <?php
    // update table where $row[$i] is empty
    ?>
    }
    </script><?
    }
    }
    this check the empty field into a databse

  7. #7
    SitePoint Enthusiast SlayMan's Avatar
    Join Date
    Oct 2004
    Location
    Germany/ Hamburg
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    U r getting away from my problem
    i do have a submit. i want to validate the from beforehand.
    in order to do so i want to check up if the field named client which is entered by the user is already in the database (this i want to be done WITH the form validation)
    my problem ist not the form validation itself. Its the fact that javascript is unable to use a mysql database.
    Therefore i want to use php which MUST be included in that javascript and i need to exchange vars between those both scripts
    THIS is my problem.
    I hope now u know what i try to create ...
    SlayMan,
    Your friendly neighbourhood trainee and webmaster
    go, see for yourself @ http://www.alpupp.de

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    // select from db
    $row = mysql_fetch_array($ret);
    if(strtolower($row[2]) == strtolower($_POST["client"])) {
    ?><script>
    if(confirm("The value alreadt exsist! Do you want submit it again?")) document.form.submit();
    </script><?
    }

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless you load the whole table into a JavaScript array, how is JavaScript going to know whether the value already exists before you submit?

    Having said that, you might want to look into XMLHttpRequest, which lets modern browsers go get values from a server-side script when some event happens.


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
  •