Jump to content

Proper method for form & function with variable validation


airborne305

Recommended Posts

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>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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 by hakimserwa
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  1. btnGen, exicuting code to return variables to later use -- Works!
  2. btnGen, writing variabls to DB -- Works!
  3. "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

  1. $newAddress --> populate txtAddr in index.php
  2. $Key --> populate txtKey in index.php
  3. $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
	}
	
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.