Ruby mechanize - discriminate two forms with their content - ruby

I have two forms with the same action, and submit button text. Only the text inside changes
<li>
<form name="login" method="post" action="">
<input name="returnURL" value="/cap/dashboard/home" type="hidden">
<input name="destURL" value="" type="hidden">
<button name="login" type="submit" class="btn-primary">
<span aria-hidden="true">Continuer</span>
</button>
<h2>textA</h2>
</form>
</li>
<li>
<form name="login" method="post" action="">
<input name="returnURL" value="/cap/dashboard/home" type="hidden">
<input name="destURL" value="" type="hidden">
<button name="login" type="submit" class="btn-primary">
<span aria-hidden="true">Continuer</span>
</button>
<h2>textB</h2>
</form>
</li>
How can I submit the right form ?

You can use form_node to search for css/xpath:
page.forms.find{|f| f.form_node.at('h2:contains("textB")')}
It wouldn't matter though, in your example both forms do the same thing.

Related

How to redirect form to different url on different button click

i am making a blog on Laravel and i am trying to redirect form to different url based on different button click . you can also refer my code
<form action="{{url('/storepost')}}" id="submitform" method="POST" enctype="multipart/form-data">
#csrf
<input type="text" name="blogtitle" placeholder="Enter the blog Title" class="form-control" >
<textarea class="form-control" id="editor" name="editor" rows="3"></textarea>
<button class="btn btn-success" type="submit">Publish</button>
<span><button class="btn btn-warning">Save as Draft</button></span>
</form>
as i have two buttons that is publish and save as draft and i want to redirect the form to different url based on button clicks , i don't understand how to do this .
can anybody help me out on this .
Thanks .
You can submit your form with a name and in the controller check for the name of the input.
<form action="{{url('/storepost')}}" id="submitform" method="POST" enctype="multipart/form-data">
#csrf
<input type="text" name="blogtitle" placeholder="Enter the blog Title" class="form-control" >
<textarea class="form-control" id="editor" name="editor" rows="3"></textarea>
<input class="btn btn-success" type="submit" value="publish" name="publish" />
<input class="btn btn-success" type="submit" value="Save as Draft" name="draft" /></span>
</form>
And in the controller check for the input:
Controller
if(\request()->has('draft')){
\\ Do draft
} else {
\\ Do publish
}
There is a way you could work around with it
you need to change your button tag to input:submit and add a name="type" for example
and in your backend, you could check this value
<form action="{{url('/storepost')}}" id="submitform" method="POST" enctype="multipart/form-data">
#csrf
<input type="text" name="blogtitle" placeholder="Enter the blog Title" class="form-control" >
<textarea class="form-control" id="editor" name="editor" rows="3"></textarea>
<input type="submit" class="btn btn-success" name="type" value="Publish">
<span><input type="submit" class="btn btn-warning" name="type" value="Save as Draft"></span>
</form>
after that in your controller, you could simply check the value like this
public function store(Request $request){
if($request->input('type') == 'Publish'){
// do something
} else {
// do another thing
}
}

I can't get back()->withInput() to work, the documentation seems a little sparse on how it should work

I'm using blade templates and this is how my form looks:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" />
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>
Does back()->withInput() only work with {{ Form::open() }}? If so, the 7.x documentation doesn't say that it seems. I would think if you need a 3rd party library to get this to work, the documentation should say so. If that's not the issue, then what am I doing wrong?
I'm using Laravel 7.x.
if you use html collective, it automatically puts old values in form inputs. But when use pure html to create form inputs you have to use old method for input values. like this:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" value="{{old('email_address')}}"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" value="{{old('password')}}"/>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>

Select/Option Laravel 5.3

I want to use select and option in laravel 5.3 in simple HTML form. How to use select and option in laravel 5.3 in HTML form?
My coding:
#extends('layout.navigation')
<html>
<head>
</script>
</head>
<div class="row" id="contact_form">
<div class="col-sm-4">
<form action="{{route('online_form')}}" method="post" enctype="multipart/form-data">
{!!csrf_field()!!}
<br/><br/>
<div class="panel panel-default" id="form_panel">
<div class="panel-body">
<p>fill the form </p>
<input type="text" name="username" class="form-control" placeholder="Name" required>
<br/>
<input type="text" name="email" class="form-control" placeholder="Email" required><br/>
<p style="margin-top:20px;font-size:14px">Gender :&nbsp&nbsp&nbsp&nbsp <label
class="radio-inline"><input type="radio" style="margin-top:1px" name="gender"
value="Male">Male</label>
<label class="radio-inline"><input type="radio" style="margin-top:1px"
name="gender" value="female">Female<br/></label>
<p style="margin-top:20px;font-size:14px"> Select Language
:&nbsp&nbsp&nbsp&nbsp<label class="checkbox-inline"><input type="checkbox"
name="language" value="english">English</label>
<label class="checkbox-inline"><input type="checkbox" name="language"
value="hindi">Hindi</label>
<br/><br/>
<select id="state">
<option value="rajasthan">Rajasthan</option>
<option value="madhya pradesh">Madhya Pradesh</option>
</select>
<input type="submit" name="submit" value="Enter" class="btn btn-primary" >
</form>
</div>
</div>
To get the state value in the controller method, you can use Input facade.
$state = Input::get('state');
You can include the following as a dependency in composer "laravelcollective/html": "~5.0" and then include it in your config/app
Now, you can create your select dropdown using:
echo Form::select('gender', array('Male', 'Female'));
echo Form::select('language', array('English', 'Hindi'));
and so on.
For more information on this collective,
check this out.

CasperJs, how to fill a form that only have a class, not a name and not an id?

I want to fill this form, but i dont know how to do it since it only have a classname.
All the examples i saw have an id or a name, to fill the form and submit it, please help.
<form class="header_form" method="post" action="">
<div class="lgn-items">
<div class="login_item">
<label>Email</label>
<input type="text" name="email" tabindex="1" class="inputtext" id="email" />
</div>
<div class="login_item" >
<label>Password</label>
<input type="password" name="password" tabindex="2" class="inputtext" id="pass" />
</div>
<div class="lgn-add">
Registration <span>|</span>
Forgot your password ?
<div class="rembo">
<input type="checkbox" name="remember" value="1" /> Remember me
</div>
</div>
</div>
<div class="login_item lgn-btn" >
<input type="submit" name="login_button" value="Login" tabindex="3" class="login" />
</div>
</form>
You can access to your form using any selector. In your case you to it like this.
casper.then(function () {
casper.fill('form.header_form', {
/** Your parameters here **/
}, true);
});

Why if i have more than 1 form in the same page with the same input file ID it is take just the first ID?

I have more than form in the same file like
<form name="myForm">
<input type="hidden" value="1" id="lastphp">
<a onclick="ajaxFunction()" class="folloo">
</form>
<form name="myForm">
<input type="hidden" value="2" id="lastphp">
<a onclick="ajaxFunction()" class="folloo">
</form>
<form name="myForm">
<input type="hidden" value="3" id="lastphp">
<a onclick="ajaxFunction()" class="folloo">
</form>
<form name="myForm">
<input type="hidden" value="4" id="lastphp">
<a onclick="ajaxFunction()" class="folloo">
</form>
but when I click on any link it always take the first ID which is 1??
An ID needs to be unique to the page, as it is a key for that element.
Why not change id="lastphp" to class="lastphp" and pass the form to the function?
<form name="myForm">
<input type="hidden" value="1" class="lastphp">
<a onclick="ajaxFunction(this.parentNode)" class="folloo">Test</a>
</form>
<form name="myForm">
<input type="hidden" value="2" class="lastphp">
<a onclick="ajaxFunction(this.parentNode)" class="folloo">Test</a>
</form>
<form name="myForm">
<input type="hidden" value="3" class="lastphp">
<a onclick="ajaxFunction(this.parentNode)" class="folloo">Test</a>
</form>
<form name="myForm">
<input type="hidden" value="4" class="lastphp">
<a onclick="ajaxFunction(this.parentNode)" class="folloo">Test</a>
</form>
And haddle it like this
function ajaxFunction(form) {
var lastphp = form.getElementsByClassName("lastphp")[0].value;
alert(lastphp);
}
This can be alot easier with a framework, like jQuery.

Resources