Page 1 of 1

echo'(if confirm("blah blah - again

Posted: Wed Dec 11, 2002 4:38 am
by Deddog
Is it possible to combine php with dynamic javascript. Or do i need a rethink?

If an entered name is already on the database, then i want to make the user aware of this and give them
the option not to input the data. I have left this verification until after the input form has been submitted.

The problem seems to lie with having a call to a php function mixed in with the Javascript.
I can't get my head round this.

The form has previously been submitted, the data is on the server, so why can't php and java code live in harmony?

I take out the "echo" and the "Addcontacts" and the javascript works.
I leave them and the java don't work but the php does - doh!

Many thanks for help and advice in advance.


// checks if the name is already in the database. If it is already there then
// it return how many times.

if ($NameExists >=1):
echo ' <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
if (confirm("'.$NAME.' already exists in the database '.$NameExists.' times Do you wish to continue?")){ ';
//
echo "<font face=Arial size=+0 color=\"#CC0000\"><b>$NAME has been added to the database</b></font><br>";
AddContacts($FName,$LName,$Spd,$Mbl,$E_M,$WPhone,$Department,$JTitle,$EasyList);
//
echo ' } </script> ';
else:
echo "<font face=Arial size=+0 color=\"#CC0000\"><b>$NAME has been added to the database</b></font><br>";
AddContacts($FName,$LName,$Spd,$Mbl,$E_M,$WPhone,$Department,$JTitle,$EasyList);
endif;

Posted: Wed Dec 11, 2002 6:08 am
by volka
always think about how php works when used with a webserver/http.
  • The client sends a request (request_header, request_body)
  • the server tries to handle the request
  • in case of a php-script the php-module/cgi is invoked
  • the script generates output (of any kind)
  • this repsonse (response_header, response_body) is sent back to the client
  • the client takes the whole response and interprets it
  • in case of html containing javascript it might be executed client-side
  • the cycle can start again....
The part "takes the whole response" isn't completely correct since most browsers start interpreting the response as soon as they can but for the beginning this is sufficient. Between the cycles there is no connection between the client and the server. Only a new http-request creates a new connction and results in a new document.

Posted: Wed Dec 11, 2002 10:51 am
by Deddog
So, are saying that i need to re-submit the data?

i.e
if confirm then form submit,
then have a "isset" to run the php function?

Posted: Wed Dec 11, 2002 2:22 pm
by puckeye
If you don't have a lot of names in your database you could always load them up in your JavaScript code on the form page. Then onSubmit check if the name is there or not and how many times it appears.

Sorta like this:

Code: Select all

<?php
// result from a query returning the names held in the DB and how many
// times they are held

while (go through the whole query result)
{
    $TheNames[$row["Names"]] ++;
    // each name will be incremented by one each time it comes up.
};

// lets make lists such as Albert,John,Puckeye,Steven,
// and 2,3,1,1     number of time for each.
while (list($Name, $Many) = each($TheNames))
{
    $Names .= $Name.",";
    $How_Many .= $Many.",";
}

// Let's cut the last comma
$Names = substr($Names, 0, -2);
$How_Many = substr($How_Many , 0, -2);

?>
<SCRIPT>

    var Names = new Array(<?=$Names;?>);
    var How_Many = new Array(<?=$How_Many;?>);

function check_name()
{
    var TheName = document.form_name.field_name.value;
    for (var i =0; i < Names.length, i++)
    {
        if (TheName.toLowerCase = Names[i].toLowerCase)
        {
             return (confirm(Names[i] + " is used " + How_Many[i] + " times, do you want to continue?"));
             break;
        }
    }

// if the execution comes to here then the name wasn't already in the DB.
    return true;
}
You call the function with your submit button, if the function returns true (either by the user say OK or because the name wasn't found) the form is submitted. If the user CANCELs the confirm part the function will return false and the form won't submit...

Of course this will be quite slow if you have thousands upon thousands of names...

The code wasn't tested in any way so I'm not sure if it works but it should.

Posted: Thu Dec 12, 2002 7:18 am
by Deddog
thanks for that Puckeye.

didn't use your code but its and your logic, knocked me back on course.

Thanks again.

Posted: Thu Dec 12, 2002 9:38 am
by puckeye
But then again I thought about that a little more, the spirit of the Programmer God possesed me... :D

You could also post your form result to a pop-up window. In this pop-up you would have a hidden form (identical fields) and a query in the DB to see how many times, if any, the name was chosen. There you give 2 buttons, one that submits the form to the proper PHP page for storing, and the other which simply closes the pop-up.

The original form would stay visible until the user click "YES" on the pop-up, clicking YES would close the pop-up and post the form to your PHP page.

Just throwing an idea your way...