airborne305 Posted January 3, 2014 Share Posted January 3, 2014 Hello! Second post here. I'm new to PHP and have an idea of what needs to be done, but im not sure the best way to impliment it. Basically im looking for direction on whether I should use JS, AJAX, Jquery, or something else. from browsing around im guessing its going to be a combination of AJAX and Jquery to get this accomplished. Please advise on the best method to acomplish this. Thanks =) The user needs to populate txtAddr and hit btnGen. The function will then confirm txtAddr is equal to a variable. If it is equal, populate other 2 text fields (txtKey & txtDest) with predefined variables. <form action="frmGen" method="post"> <input name="txtAddr" type="text"> <!-- User enters text here -- Confirm txtAddr.text = $VarAddr -- If True, continue. If False, display DIV message --> <input name="txtDest" type="text"> <!-- Text field is filled with value from server/SQL when btnGen is pressed--> <input name="txtKey" type="text"> <!-- Text field is filled with value from server/SQL when btnGen is pressed--> <input name="btnGen" type="button"> <!-- assuming txtAddr is True, display strings in 2 text fields above & store all values from 3 text boxes in SQL --> </form> Quote Link to comment https://forums.phpfreaks.com/topic/285050-proper-method-for-form-function-with-variable-validation/ Share on other sites More sharing options...
mac_gyver Posted January 3, 2014 Share Posted January 3, 2014 you must get your form and your form processing code to work at all, first, before you can add something like using AJAX to submit data without refreshing the page, because even with AJAX, the only thing the client side code can do is make a http request to the server side code, so the same functionality and same root code must exist regardless of using AJAX or not. here's a little http client/server 101. the client side code cannot directly call functions in or set variables in the server side code. all it can do is make a get or post http request to the server side code. it is then up to the server side code to have logic that looks at the $_GET or $_POST variables it receives as input and decides what to do with them. Quote Link to comment https://forums.phpfreaks.com/topic/285050-proper-method-for-form-function-with-variable-validation/#findComment-1463665 Share on other sites More sharing options...
hakimserwa Posted January 3, 2014 Share Posted January 3, 2014 (edited) <form action="frmGen" method="post"> <input name="txtAddr" type="text"> <!-- User enters text here -- Confirm txtAddr.text = $VarAddr -- If True, continue. If False, display DIV message --> <input name="txtDest" type="text" style="display:none"> <!-- Text field is filled with value from server/SQL when btnGen is pressed--> <input name="txtKey" type="text" style="display:none"> <!-- Text field is filled with value from server/SQL when btnGen is pressed--> <input name="btnGen" type="button"> <!-- assuming txtAddr is True, display strings in 2 text fields above & store all values from 3 text boxes in SQL --> </form> javascript using jquery function check_txtAddr(){ var txtAddr = $("form input[name=txtAddr]").val(); // post the value to a php script that will check if txtAddr = to the valuable you are checking. // if it does show the other two input fields. // use the jquery post function. $.post(check_txtAddr.php, {txtAddr:txtAddr}, function(data) { // catch the response data from the php file var txtDest = data['txtDest']; var txtKey = data['txtKey']; if (txDest !== ""){ $("form input[name=txDest]").attr("value", txDest); $("form input[name=txDest]").show(); } if (txtKey !== ""){ $("form input[name=txtKey]").attr("value", txDest); $("form input[name=txtKey]").show(); } }, 'json'); } $("document").ready(function() { $("form input[name=btnGen]").click(check_txtAddr); }); php script check_txtAddr.php if (isset($_POST['txtAddr'])){ // check if $_POST['txtAddr'] == to the valuable you want to check if ($_POST['txtAddr'] == $your_valuable){ //prepare the response return value to jquery $txtKey = //what ever you want to populate in that field $txDest = //what ever you want to populate in that field // return the response by echoing it out as json encoded data for simplist make it an array. echo echo json_encode(array('txtKey'=>$txtKey,'txDest'=>$txDest)); } } Try it its not tested but i believe it can give you a starting idea on what you have to do. Thing that you need to make more research about how they work is jquery post method, json. good luck Edited January 3, 2014 by hakimserwa Quote Link to comment https://forums.phpfreaks.com/topic/285050-proper-method-for-form-function-with-variable-validation/#findComment-1463671 Share on other sites More sharing options...
trq Posted January 3, 2014 Share Posted January 3, 2014 whether I should use JS, AJAX, Jquery, or something else By the sounds of it you need to understand what each of these technologies are. 1) Javascript is a programming language 2) Ajax is a technique created using Javascript. 2) JQuery is a framework written in the Javascript programming language that can be used to implement Ajax. Quote Link to comment https://forums.phpfreaks.com/topic/285050-proper-method-for-form-function-with-variable-validation/#findComment-1463674 Share on other sites More sharing options...
airborne305 Posted January 10, 2014 Author Share Posted January 10, 2014 Hi folks, thanks for the responses. I made a little progress... quite a bit actual, I think. I got the form setup and PHP writing to the DB. I played around with hakimserwa's code a bit, but was unable to have any luck getting any of the JavaScript to work. Also, with the onClick, how would i prevent it from leaving index.php and going to frmgen.php ? Heres what I accomplished since my original post. btnGen, exicuting code to return variables to later use -- Works! btnGen, writing variabls to DB -- Works! "confirm txtAddr is equal to a variable" -- Ended up using a JSON response, $isvalid below. Works! The variables that im interested in from frmgen.php are the 3 below. I would like to have those variables populate the labls and text fields in index.php $newAddress --> populate txtAddr in index.php $Key --> populate txtKey in index.php $fee --> populate txtFee in index.php index.php <form id="frmGen" method="post" action="frmgen.php" > <label>Destination Address:</label> <input name="txtDest" type="text" maxlength="34" class="txt" value"1234567890QWERTYUIOP0987654321WWWX"> <label>Random Key:</label> <input name="txtKey" type="text" maxlength="10" class="txt" value"1234567890"> <label>Address:</label> <label name="txtAddr" class="txt"></label> <label>Fee:</label> <label name="txtFee" class="txt"></label> <label>Generate Address:</label> <input name="btnGen" type="submit" value="Generate Address"> </form> frmgen.php <?php $DestAddr = $_POST['txtDest']; include("RPCConnect.php"); $isvalid = $RPCtool->validateaddress($DestAddr); $account = ("queue"); # Variable just incase needed later if ($isvalid['isvalid'] == "1") { echo "valid"; echo("<br>");#Continue code here. echo $newAddress = $RPCtool->getnewaddress($account); echo("<br>"); #Store $newAddress in DB echo $Key = ("RandomKey"); echo("<br>"); #Store vKey in DB echo $fee = mt_rand (15,30)/10, "%"; echo("<br>"); #Store fee in DB include("sqlcon.php"); mysqli_query($con,"INSERT INTO TABLNAME (Key, ipAddr, LocalAddr, DestAddr, fee) VALUES ('$Key', '$ip','$newAddress', '$DestAddr', '$fee')"); mysqli_close($con); } else{ echo "This is not a valid string. Please provide a valid address."; #ooples } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285050-proper-method-for-form-function-with-variable-validation/#findComment-1464659 Share on other sites More sharing options...
mac_gyver Posted January 10, 2014 Share Posted January 10, 2014 in general, when you want to request a page with specific existing information displayed on it, you would request that page with the id of the information in the url and the page would retrieve and display the corresponding data. i would get the last inserted id after the insert query is ran and redirect to the form page with that id on the end of the url. Quote Link to comment https://forums.phpfreaks.com/topic/285050-proper-method-for-form-function-with-variable-validation/#findComment-1464719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.