how to pass parameter to php function and call in html - php

I have this function in php; a separate file, function dbRowInsert($table_name, $form_data).
I included it in my php file in which registration happens. My problem is how do I call the function on form submit and pass a parameter to the dbRowInsert function. This is the data of my form:
$form_data = array(
'username' => $username,
'password' => $password,
'title' => $title,
'first_name' => $first_name,
'middle_name' => $middle_name,
'last_name' => $last_name,
'position' => $position,
'residence' => $residence,
'monthly_salary' => $monthly_salary,
);
I tried this method:
<form id="signup_form" class="form-horizontal" role="form" action="<?php dbRowInsert(tblperson, $form_data) ?>">
...
</form>

PHP is not written like JavaScript; a POST request must be sent to a PHP page for processing (unless you're using AJAX), like so
<form method="POST" action="process.php">
....
</form>
In process.php, you have to extract out the fields you want to send to the function.
$username = $_POST['username'];
doSomethingWIthUserName($username);
Or in you case, since you are sending the entire array:
dbRowInsert("tblHelpers", $_POST);
Here's a detailed tutorial on handling POST requests.

Put all that in the same file where register form is after including function php file
<?php
//include file code start
function dbRowInsert($tblperson, $form_data){
echo "<pre>".print_r($form_data,true)."</pre>";
}
//include file code end
if(isset($_POST['submit'])){
$form_data = array();
$form_data['username'] = $_POST['username'];
$form_data['password'] = $_POST['password'];
$tblperson = "person";
//do the action
dbRowInsert($tblperson, $form_data);
}
?>
<form id="signup_form" method="post" class="form-horizontal" role="form" action="">
<input type="text" name="username" />
<input type="text" name="password" />
<input type="submit" name="submit" />
</form>

Related

Request form and POST in a single go?

Please excuse the poor title.
I am a total beginner and don't know the right terms to make it better.
I am trying to POST form data using PHP.
My problem is that before i POST the form data i need to get a value from the form, that is changing each time i request the page.
Please notice the second input, the value is auto generated and is a random number each time i request the form.
Here is my form.php:
<?php
if(!isset($_REQUEST['submit_btn'])){
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<input type="text" name="user_name" id="user_name">
<input type="hidden" name="a_random_password" value="'.(rand(10,100)).'">
<input type="submit" value="submit" name="submit_btn">
</form>';
}
if(isset($_REQUEST['submit_btn']))
{
$user_name= $_POST["user_name"];
$a_random_password = $_POST["a_random_password"];
echo "Your User Name is:". $user_name;
echo "<br> and your Password is : $a_random_password;
}
?>
And my post.php
<?php
$url = "http://test.com/form.php";
$data = array(
'user_name' => 'John',
'a_random_password' => 'xxx',
'submit' => 'submit'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "Error"; }
var_dump($result);
?>
So how do i get the a_random_password value and submit it along with the form in a single request, else the password wont fit with the user name.
Sup, what you wanna do is impossible using PHP in that way, cause the only job PHP has is to render your content on server side. To listen to the client side you'll need to use javascript.
let randomPassword = document.getElementById('a_randow_password')
let name = document.getElementById('name')
let password = document.getElementById('password')
name.onkeyup = function(){
password.value = randomPassword.value + name.value
}
<input type="text" id="name" name="user_name" placeholder="name"><br />
<input type="hidden" name="a_random_password" id="a_randow_password" value="xxx">
<input type="text" id="password" placeholder="password"><br />
Not sure exactly what you trying to achieve, but if You want to interrupt a request (and modify|use it) from HTML to PHP, You need AJAX request (JavaScript).

Url from form using php

I am trying to create a link to include variables from a form.
The script below works put only includes the first variable:
http://example.com/abc.php?id=2
I want it to send:
http://example.com/abc.php?id=2&name=zac
PHP code shown below:
$base = 'http://example.com/abc.php';
$id=$_GET['ID'];
$name=$_GET['Name'];
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;
You could make your life a bit easier by just sending the form via $_GET and it will redirect to the URL like you're wanting. Here's an example:
<form action="http://example.com/abc.php" method="GET">
<input type="text" name="ID" />
<input type="text" name="Name" />
</form>
This would send the user to: http://example.com/abc.php?ID=id_value&Name=name_value
Note: This will send all form variables with a value set.

Why Wordpress wp_update_post function removes html form tags?

I'm trying to add a form with hidden inputs into the post content using external PHP fle.
When i try it from the browser the form added successfully but when i try it from command line, the form inputs are deleted.
Here is my code:
require('../wp-load.php');
$content = '<div class="buy-preowned">
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
<input name="CPOvehicle" type="text" value="1"/>
<input name="VIN" type="hidden" value="5YJSA1E1XHF210809"/>
<input name="vehicleMapId" type="hidden" value="1280359"/>
<input name="titleStatus" type="hidden" value="NEW"/>
<input name="form_id" type="hidden" value="tesla_cpo_marketing_buy_form"/>
</div>
</form>
<p class="small-text">
Requires a $2,500 deposit
</p>
</div>
';
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
When i checked the database, the form is stored as:
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
</div>
</form>
this is only happen when i rub the script from command line, any idea how to resolve it.
Thank you
It s because of built-in security filters. As it is not normal to store some form data inside content(there are shortcodes for that), WP disabled inserting some non-text tags.
But anyway, you can enable it manually.
kses_remove_filters();
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
wp_update_post($data);
kses_init_filters();

Why is my form not being submitted?

I have 2 functions - one generates the form on my main page and the other processes the submitted form. This is the Braintree sandbox API and their method is this: take in user info and submit to Braintree server, BT server returns a payment method nonce to me which I can then use to POST and view the transaction in my sandbox control panel. However, the form isn't being submitted and I'm not sure at what point in the process the whole submission is failing. NOTE - I am submitting the form to the same PHP file where the form is located.
I still need help on this...
ask.php - This is the page where I call both functions
<div>
<?php
fd_bt_form();
fd_process_trans();
?>
</div>
find-do-for-anspress.php
$FD_Braintree_Keys = array(
Braintree_Configuration::environment('sandbox'),
Braintree_Configuration::merchantId('A'),
Braintree_Configuration::publicKey('B'),
Braintree_Configuration::privateKey('C')
);
function fd_bt_form()
{
$class_bt_token = new Braintree_ClientToken();
$clientToken = $class_bt_token->generate();
?>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
braintree.setup(
'<?php echo $clientToken ?>',
'custom', {
id: 'checkout',
});
</script>
<?php
echo
'<form id="checkout" action="" method="POST">
<p>
<label><font size="5">Amount:</font></label>
<input type="text" size="4" name="amount" id="amount" />
</p>
<input data-braintree-name="number" value="378282246310005">
<br> <br />
<input data-braintree-name="expiration_month" value="05">
<input data-braintree-name="expiration_year" value="17">
<br> <br />
<input data-braintree-name="cvv" value="531">
<br> <br />
<input type="submit" id="submit" value="Pay">
</form>';
echo $_POST["payment_method_nonce"];
global $bt_nonce;
$bt_nonce = $_POST["payment_method_nonce"];
return $bt_nonce;
}
function fd_process_trans() {
$FD_Braintree_Keys;
$nonce = $_POST["payment_method_nonce"];
$amount = $_POST["amount"];
$result = Braintree_Transaction::sale(array(
'amount' => $amount,
'paymentMethodNonce' => $nonce,
'options' => array(
'submitForSettlement' => True,
),
));
if ($result->success) {
echo "Success!";
}
else {
echo "Transaction failed.";
}
}

Send HTML input to PHP Twilio Script

I'm new to PHP and HTML forms. I'm trying to send the form data below to my PHP script sendsms.php it doesn't seem that the php script is getting the input data from the html form.
<form action="sendsms.php" method="post" >
<div id="space">Cell:<div><input type="number" name="phone" id="number" ></div></div>
<div id="space"><div>Message</div><div><input type="text" name="message" id="message"> </div></div>
<div id="button"><input type="submit" name="send" value="Send" id="button" ></div>
</form>
Here is my PHP file sendsms.php
<?php
// this line loads the library
require('twilio-php/Services/Twilio.php');
$account_sid = 'REMOEVD';
$auth_token = 'REMOVED';
$client = new Services_Twilio($account_sid, $auth_token);
$client->account->messages->create(array(
'To' => $_POST['phone'];,
'From' => "+16194523868",
'Body' => $_POST['message'];,
));
If you look at your form:
<form action="sendsms.php" method="post" >
you are essentially posting the form inputs, so you need to get the $_POST parameters in PHP as in :
$client->account->messages->create(array(
'To' => $_POST['phone'],
'From' => "+16194523868",
'Body' => $_POST['message']
));
Edit
Just noticed you also have some semicolons (;) inside the array declaration which will still break your code

Categories