Hello i am after creating a javascript alert box with a text input and a submit button that when the submit button is pressed the text field will save its self into a php variable that will then be inserted into the a database.

here is the code i have so far

if($Petname == '')
{ ?>
<body onload='javascript: jQuery.facebox("<div align=\"center\">Enter your pets Name:&nbsp;<input type=\"text\" name=\"pet\" ><br /><br /><input type=\"submit\"></div> ");'></body>
<?php }

how would i retrieve the variable from the javascript and store it into php?

Recommended Answers

All 2 Replies

You need another PHP script to perform the MySQL queries.

function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            return new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e) {}
    }
    return false;
}

var http = getHTTPObject();
http.open('POST', 'http://www.example.com/script.php?var='+value, true);
http.send();
Member Avatar for diafol

As you're using jQuery, your can forgo the HTTPObject stuff.

You mention an alert box - this is not an alert box in the usual sense, it's more like a js modal form. If this is a security thing
- i.e. it must pop up under certain circumstances, have a server-side backup as it probably won't degrade well if the user doesn't have js enabled.

As mentioned, you prbably wan to use Ajax for this, otherwise you'll have to refresh or redirect the page due to form submit - although you don't seem to have a form, just a few controls in a div.

Have a look at jQuery .post and jQuery .ajax for examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.