How to remove specific data line from html file with Ruby - ruby

i have a file name payment.html.erb
content have:
<form method="POST" action="http://www.example.com" id="my_id" class="form">
<input type="hidden" name="Timestamp" value="2013-09-29T08:05:14.Z"/>
<input type="hidden" name="Signature" value="dd01adafd2689b243d6cbc9088da2bf699976eb0"/>
<input type="hidden" name="Amount" value="1"/>
<input type="text" name="AccountName" value="" placeholder="account name"/>
<p></p>
<select name="ExpireMonth">
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select name="ExpireYear">
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" class="yyy" id="xxx" value="submit"/>
</form>
i am reading from file and again need to write back into file (i already coded)
i want remove all non hidden html fields and also 'Form' starting tag and closing tag.
thanks

You should use a parser to manipulate HTML or XML unless the content is trivial and you have complete control of it. If you don't own it, or it's not trivial, there's too many things that can go wrong if it chances, which will cause your code to break and either crash, or mess up the markup.
Instead, I'd use Nokogiri. It's an excellent parser for XML and HTML, and can make short work of what you're trying to do:
html =<<EOT
<form method="POST" action="http://www.example.com" id="my_id" class="form">
<input type="hidden" name="Timestamp" value="2013-09-29T08:05:14.Z"/>
<input type="hidden" name="Signature" value="dd01adafd2689b243d6cbc9088da2bf699976eb0"/>
<input type="hidden" name="Amount" value="1"/>
<input type="text" name="AccountName" value="" placeholder="account name"/>
<p></p>
<select name="ExpireMonth">
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select name="ExpireYear">
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" class="yyy" id="xxx" value="submit"/>
</form>
EOT
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css('input[type!="hidden"]').remove
form_contents = doc.at('form').children
doc.at('form').replace(form_contents)
puts doc.to_html
Running that outputs:
<input type="hidden" name="Timestamp" value="2013-09-29T08:05:14.Z"><input type="hidden" name="Signature" value="dd01adafd2689b243d6cbc9088da2bf699976eb0"><input type="hidden" name="Amount" value="1"><p></p>
<select name="ExpireMonth"><option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option></select><select name="ExpireYear"><option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option></select>
A parser such as Nokogiri can handle that without problems.
In addition, a parser can handle this valid markup:
<input
type="text"
name="AccountName"
value=""
placeholder="account name"
/>
Try using a regular expression and gsub to strip that or this:
<input type="text"name="AccountName"value="<your name goes here>"placeholder="account name"/>

Read the file into a string.
Use gsub() to remove the unwanted tags.
Write the string to the file.
#!/usr/bin/env ruby
file_name = 'payment.html.erb'
data = IO.read(file_name)
data.gsub!(/<input.*type="hidden".*>/, '')
data.gsub!(/<form.*>/, '')
data.gsub!(/<\/form>/, '')
File.open(file_name, 'w') {|f| f.write(data) }
Update
I misread the question. This will remove all input tags that are not hidden:
#!/usr/bin/env ruby
file_name = 'payment.html.erb'
data = IO.read(file_name)
copy = String.new(data)
copy.scan(/<input.*>/) { |tag|
data.gsub!(tag, '') if !tag.include?("type=\"hidden\"")
}
data.gsub!(/<form.*>/, '')
data.gsub!(/<\/form>/, '')
File.open(file_name, 'w') {|f| f.write(data) }
It can be easily modified to remove other types of non-hidden tags.

Related

How to insert multiple checkbox data to database using Codeigniter

Can somebody help me? I have question. I have view, name is pascasarjana.php
<div class="form-group">
<label>Program studi dan konsentrasi yang diminati</label><br/>
<input name="prodis" type="checkbox" value="p1">Program Studi Magister Manajemen<br/>
</div>
<div class="form-group">
<input name="prodis" type="checkbox" value="p2">Program Studi Magister Teknik Sipil
<select name="konsentrasi" class="form-control" id="konsentrasi1">
<option value="">Pilih Konsentrasi</option>
<option value="k1">Manajemen Konstruksi</option>
<option value="k2">Transportasi</option>
<option value="k3">Struktur</option>
</select>
</div>
<div class="form-group">
<input name="prodis" type="checkbox" value="p3">Program Studi Magister Ilmu Hukum
<select name="konsentrasi" class="form-control" id="konsentrasi2">
<option value="">Pilih Konsentrasi</option>
<option value="k4">Hukum Bisnis</option>
<option value="k5">Hukum Ketatanegaraan</option>
<option value="k6">Hukum Agraria</option>
<option value="k7">Litigasi</option>
</select>
</div>
<div class="form-group">
<input name="prodis" type="checkbox" value="p4">Program Studi Magister Teknik Informatika
<select name="konsentrasi" class="form-control" id="konsentrasi3">
<option value="">Pilih Konsentrasi</option>
<option value="k8">Soft Computing</option>
<option value="k9">Enterprise Information System</option>
<option value="k10">Mobile Computing</option>
</select>
</div>
<div class="form-group">
<input name="prodis" type="checkbox" value="p5">Program Studi Magister Teknik Arsitektur
<select name="konsentrasi" class="form-control" id="konsentrasi4">
<option value="">Pilih Konsentrasi</option>
<option value="k11">Arsitektur Digital</option>
</select>
</div>
AND I confused, how to insert multiple checkbox and select option data to database in my controller and model.
Try renaming your HTML entities as arrays
Eg.
input name="prodis" to input name="prodis[]"
select name="konsentrasi" to select name="konsentrasi[]"
Then print $_POST in your controller.
I guess you need to get data from select box on the basis of ticked checkbox.
For getting values from checkbox you need to define checkbox name as an array. In my code I am using exact indexes so we can get select box values accordingly.
I guess below first checkbox there should be another select box. Ignoring this select box (Which is actually not here), I am giving custom indexes here. Instead, we also can use like profis[] which actually auto generate indexes by incrementing it one on each occurrence in HTML
You can implement validation too. Here are modified code:
<div class="form-group">
<label>Program studi dan konsentrasi yang diminati</label><br/>
<input name="prodis[1]" type="checkbox" value="p1">Program Studi Magister Manajemen<br/>
</div>
<div class="form-group">
<input name="prodis[2]" type="checkbox" value="p2">Program Studi Magister Teknik Sipil
<select name="konsentrasi[2]" class="form-control" id="konsentrasi1">
<option value="">Pilih Konsentrasi</option>
<option value="k1">Manajemen Konstruksi</option>
<option value="k2">Transportasi</option>
<option value="k3">Struktur</option>
</select>
</div>
<div class="form-group">
<input name="prodis[3]" type="checkbox" value="p3">Program Studi Magister Ilmu Hukum
<select name="konsentrasi[3]" class="form-control" id="konsentrasi2">
<option value="">Pilih Konsentrasi</option>
<option value="k4">Hukum Bisnis</option>
<option value="k5">Hukum Ketatanegaraan</option>
<option value="k6">Hukum Agraria</option>
<option value="k7">Litigasi</option>
</select>
</div>
<div class="form-group">
<input name="prodis[4]" type="checkbox" value="p4">Program Studi Magister Teknik Informatika
<select name="konsentrasi[4]" class="form-control" id="konsentrasi3">
<option value="">Pilih Konsentrasi</option>
<option value="k8">Soft Computing</option>
<option value="k9">Enterprise Information System</option>
<option value="k10">Mobile Computing</option>
</select>
</div>
<div class="form-group">
<input name="prodis[5]" type="checkbox" value="p5">Program Studi Magister Teknik Arsitektur
<select name="konsentrasi[5]" class="form-control" id="konsentrasi4">
<option value="">Pilih Konsentrasi</option>
<option value="k11">Arsitektur Digital</option>
</select>
At controller end you can just check if index 2 $this->input->post('prodis[2]') then get the value of select box with index 2 $this->input->post('konsentrasi[2]'
You can check it dynamically by keeping code redundancy in your mind. Let me know if you face any issue.
Insert Multiple data using check box or anything on HTML.Just use array "[]".
<input name="checkbox[]" type="checkbox" value="checkbox">
<select name="select[]"></select>
Can also array values like [2],[4],[5] anythings
Thank You
public function insert()
{
//Insert second stage details for employer into database.
$Specilized_category = $this->input->post('spec_cat');
$data=array(
'Specilized_category'=>json_encode(implode(",", $Specilized_category)),
);
$this->db->insert('tbl_employer', $data);

How can i make custom form module joomla3.x

<select name="A">
<option value="1">Volvo</option>
<option value="2">Saab</option>
</select>
<select name="B">
<option value="1">test1</option>
<option value="2">test2</option>
</select>
<input type="Submit" >
After I submit, I want to show result from A and B. For example:
if($_POST['A']==1 && $_POST['B']==2){
echo 'true';
}else{
echo '0';
}
Is it possible to do this in joomla?
you have to enclosed this input fields into form tag. For example
<form method="post">
<select name="A">
<option value="1">Volvo</option>
<option value="2">Saab</option>
</select>
<select name="B">
<option value="1">test1</option>
<option value="2">test2</option>
</select>
<input type="Submit" >
</form>
Hope it will help

PayPal buttons with versatile functions for Joomla

I have been doing some testing on my organization's donation page (https://www.livingmiraclescenter.org/donations.html). Currently the service that provides us the payment options is First Data; this company is changing the way they work and in order for us to continue using it, we would need to completely overhaul our donations pages across our websites. So, we're investigating how versatile and customizable PayPal buttons are. If you check the link, you see that our current options through First Data are multifaceted. In one simple transaction people may choose a preset amount to donate, pick their own amount, AND choose recurring payment options with the donation amount they chose or choose not to use that option. So, in one payment form we’re able to offer one-time donations as well as recurring ones.
I would like to know if there is any way that PayPal buttons may provide the same versatility. We've been thinking to switch over entirely to PayPal and do away with First Data, but the one thing that's held us back is the little customization that has been offered when making PayPal buttons.
I've played with different code for payment buttons, however been getting error messages ("Invalid Regular period. You must specify valid values for the A3, P3, and T3 parameters for a subscription") each time I actually try out the newly created buttons on the website. The codes I've used are, one:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="SACSLVL35UTAS">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Donate to My Organization">
<input type="hidden" name="item_number" value="1234">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="src" value="1">
<input type="hidden" name="p3" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-SubscriptionsBF:btn_subscribeCC_LG.gif:NonHosted">
<table>
<tr><td>When would you like this to Recur?</td></tr><tr><td><select name="t3">
<option value="D">Daily</option>
<option value="M">Monthly</option>
<option value="Y">Yearly</option>
</select> </td></tr>
<tr><td>How many times would you like this to recur? (including this payment)</td></tr><tr><td><select name="srt">
<option value="0">Never End</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select> </td></tr>
<tr><td>Enter Your Donation Amount</td></tr>
<tr><td><input type="text" name="a3" size="4" maxlength="60"></td></tr>
</table>
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to
pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
and two:
<form target="_self" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<!-- If using a Business or Company Logo Graphic, include the "cpp_header_image" variable. -->
<input type="hidden" name="cpp_header_image" value="https://yourwebsite.com/logo.jpg">
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<!-- <input type="hidden" name="redirect_cmd" value="_xclick-subscriptions"> -->
<!-- Replace "business" value with your PayPal Email Address or your Merchant Account ID -->
<input type="hidden" name="business" value="your email address">
<input type="hidden" name="item_name" value="Widget Fund Donation">
<input type="hidden" name="item_number" value="WFD-1002">
<input type="hidden" name="no_note" value="1">
<!-- no_shipping value 2 needed to send the free gift -->
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="tax" value="0">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="cn" value="Add special instructions to the seller:">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHosted">
<!-- Replace value with the web page you want the customer to return to after a successful transaction -->
<input type="hidden" name="return" value="http://www.yourwebsite.com/ThankYou.html">
<!-- Replace value with the web page you want the customer to return to after item cancellation -->
<input type="hidden" name="cancel_return" value="http://www.yourwebsite.com/Cancel.html">
<!-- -->
Enter the amount you would like to donate:
<!-- a3, Required, Regular Subscription Price -->
<input type="text" name="a3" size="4" maxlength="7">
<br><br>
<!-- src, Optional, Recurring Payments, 0 no recur, 1 payments recur -->
<input type="hidden" name="src" value="1">
<!-- -->
Select the length of the desired Billing Cycle:
<!-- p3, Required, Duration based on the value of t3 -->
<select name="p3">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="15">15</option>
<option value="30">30</option>
</select>
<br><br>
<!-- -->
Select how often you would like to Donate:
<!-- t3, Required, Subscription Units D W M Y, -->
<select name="t3">
<option value="D">Day(s)</option>
<option value="W">Week(s)</option>
<option value="M">Month(s)</option>
<option value="Y">Year(s)</option>
</select>
<br><br>
<!-- -->
Select the number of Donation Payments you would like to make:
<br><br>
<!-- srt, Optional, Recurring Times, must be used with src -->
<select name="srt">
<option value="">Until Canceled</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="6">6</option>
<option value="12">12</option>
<option value="18">18</option>
<option value="24">24</option>
<option value="36">36</option>
</select>
<!-- sra, Optional, Reattempt on failure 2 more time, value 0 or 1, if 0 no reattempt -->
<input type="hidden" name="sra" value="1">
<br><br><br>
Thank you for your Donation,
<br><br>
Please Select your Free Gift:
<input type="hidden" name="on0" value="Free Gift Choice">
<select name="os0">
<option value="Solar Calculator">Solar Calculator</option>
<option value="Key Chain Light">Key Chain Light</option>
</select>
<br><br><br>
<!-- -->
<input type="hidden" name="bn" value="PP-SubscriptionsBF:btn_donateCC_LG.gif:NonHosted">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
Thank you for any insight, technical help, and wisdom you can provide.
Thanks,
Jutta

Paypal form submission - mobile

Im using below code to submit values in to Paypal and its working.
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick" />
<h2 style="font-family:Segoe UI;">Your Email :</h2>
<input type="text" name="business" value="youremailaddress#yourdomain.com" style="width:1000px; height:50px; margin-left:30px;"/>
<br />
<h2 style="font-family:Segoe UI;">Driveway Size :</h2>
<input type="hidden" name="item_name" value="" />
<select id="item_price" name="amount" style="width:1000px; height:50px; margin-left:30px;">
<option value="325">Single $325.00</option>
<option value="375">Double $375.00</option>
<option value="400">2.5 $400.00</option>
<option value="425">Triple $425.00</option>
<option value="450">3.5 $450.00</option>
<option value="475">Quad $475.00</option>
</select>
<br />
<input type="submit" value="Buy!" style="margin-top:30px"/>
</form>
But this is desktop version. is there anyway that I can do same thing for mobiles?
Yes you can. You could for example use System.Net.WebClient to make the post towards the URL you have in the action field of the form.
Or, you can have the above HTML code embedded inside a web-browser control in your application.

Validate register form

I am trying make a register form adn using jquery validation. I have a problem.
I want only one "warning message" for "birthday select box"..I couldnt manage this and it look terrible when i get 3x warning for each bday select box. How can i give "one" warning message if at least one box not choosed?
And my codes are below, i made it short and working for test:
<script language="JavaScript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$.validator.addMethod("username", function(value, element) {
return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
}, "Username must contain only letters, numbers, or underscore.");
$("#regForm").validate();
});
</script>
</head>
<body>
<div>
<form method="post" id="regForm" action="register.php">
<div>
Name<br>
<input id="user_name" name="name" type="text" minlength="5" class="required username"/><br>
</div>
<div>
E-mail<br>
<input id="usr_email3" name="email" type="text" class="required email"/><br>
</div>
<div>
Password<br>
<input name="pass1" type="password" class="required password" minlength="5" id="pwd" /><br>
</div>
<div>
Confirm Password<br>
<input id="pwd2" name="pass2" class="required password" type="password" minlength="5" equalto="#pwd" /><br>
</div>
<div>
Birthday:
<select name="birthday_day" class="required">
<option value="">Day</option>
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<select name="birthday_month" class="required">
<option value="">Month</option>
<option value="1" >January</option>
</select>
<select name="birthday_year" class="required">
<option value="">Year</option>
<option value="1900" >1900</option>
</select>
</div>
<div>
Gender:
<select name="gender" class="required">
<option value="">Choose</option>
<option value="Male">Male</option>
<option value="Female">Female</option><br /></div>
<br>
<div>
<input id="doRegister" name="doRegister" type="submit" value="Send"/>
</div>
</form>
</div>
Give an id to the div surrounding the birthday tags:
<div id="required">
Birthday:
<select name="birthday_day" class="required">
<option value="">Day</option>
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<select name="birthday_month" class="required">
<option value="">Month</option>
<option value="1" >January</option>
</select>
<select name="birthday_year" class="required">
<option value="">Year</option>
<option value="1900" >1900</option>
</select>
</div>
Use the code below to apply a red border if any of the select tags are not selected:
$('#doRegister').click(function(){
var a = $('select[name="birthday_day"]').val();
var b = $('select[name="birthday_month"]').val();
var c = $('select[name="birthday_year"]').val();
if(a == '' || b == '' || c == '')
{
$('div#required').css('border', '1px solid f60f60');
}
else
{
$('div#required').css('border', 'none');
}
});
This,of course, avoids the use of the validator plugin that you're using

Resources