confusing syntax in JSP portal - javascript

JSP code in Liferay portal..
<portlet:actionURL name="addDetails" var="addDetailsURL" />
where from var gets value ?

var just exposes the action URL value through a variable named addDetailsURL. This is so that you can use it conveniently in a link as so:
Link

is used to perform any action(Action Phase) i.e user submitting form and controller(processAction) processes that request.
i.e
<form action="<%= addDetailsURL %>" method="POST" name="form">
// input fields which will get submitted to controller
</form>

from pageContext.
ActionURL.toString() is assigned to addDetailsURL and added to pageContext

Related

Is is possible to send form data to an external link with Angular?

For example, consider this code:
<form method = post action="URL">
<Input type = text name= first name />
<Input type = text name= last name/>
<Input type = submit value= submit name=submit/>
<form/>
In the code above we can see form data in headers. Can the same functionality be replicated in Angular?
To be clear, I want to pass input values to an external link via the post method in Angular.
Rakesh, if you only want to send a value -typical to an API that update a dbs- it's only using httpClient, see the docs
constructor(private:httpClient:HttpClient){}
submit()
{
this.httpClient.post('your-url',
{firstname:'yourName',lastname:'lastname'})
}
if you need to show a page that not belong to the app -typical external url to login or a virtual tpv-, you can create a form "outside Angular" adding ngNoForm to your form
<form method ="post" action="URL" ngNoForm >
</form>
Of course you can use variables

jquery the parameter passed in is null when set form action attribute

I am trying to set my form action to another page with a parameter in url using code below:
ui.setFormActionToTargetPage = function () {
$('form').get(0).setAttribute('action', 'myTargetPage.html?id='+id);
};
My html page is like
<form class="form-horizontal row-border" data-validate="parsley" id="validate-form" enctype="multipart/form-data">
...
<button class="btn-default" onclick="ui.setFormActionToTargetPage();">Cancel</button>
...
</form>
However, I did get forwarded to myTargetPage.html but it seems the id parameter is not passed while some form content was passed as parameter 'text'. I verified that when setFormActionToTargetPage is called the url is as expected. However, when it hit the target page, the window.location.href is not correct, which is 'myTargetPage.html?text= some form content'. Anyone know why?
Thanks.
Change the form method to post so the form data doesn't interfere with the querystring. If you have your id value somewhere and it's working already then great, otherwise you can pass it through the button onclick call like this.
<form method="post" class="form-horizontal row-border" data-validate="parsley" id="validate-form" enctype="multipart/form-data">
<button class="btn-default" onclick="ui.setFormActionToTargetPage(123);">Cancel</button>
</form>
ui.setFormActionToTargetPage = function (id) {
$('form').prop('action', 'myTargetPage.html?id='+id);
};

php form : twig parameter from javascript or textarea html

i have a textarea defined by an id
<textarea id='vegetable'> Tomato </textarea>
i have a button
<button type="button" onclick="MyFunction()">generate vegetable</button>
which trig a javascript in order to modify the content of the textarea
<script>
function MyFunction()
{
document.getElementById("vegetable").innerHTML = VegetableNameGenerator();
}
</script>
The problem is this php action :
<form action="{{ path('mypath', { 'myparam': ??? }) }}" method="post" >
<input type="submit" />
</form>
??? must be the content of the textarea (which is also known by the javascript code) but i don't know how to access it in twig.
I guess there are several way of doing that : jquery, dom, global variable twig... any syntax example would be great.
This is impossible in the way you're describing it.
When a request is made to the server, twig renders the page, then it is sent to the browser, and then javascript can run.
There are options for how to make this work:
Create a controller action which returns the rendered path when you call it using a GET request with the provided parameter. Then create an event listener on the submit button that blocks the submit process until it has retrieved the route via AJAX and modified the form's action attribute.
Redirect from your controller to the path you want displayed. myparam will be included in the post data, so you can redirect from your controller action after you've handled the form.
$this->redirect($this->get('router')
->generate('mypath',
array('myparam'=>
$this->getRequest()->get('myparam')
),
true);

passing javascript value into form action

I have the form:
<form name="form1" method="post" action="test.php?date=javascript:this.form.date2.value">
I am using a DatetimePicker for PHP like below:
$myCalendar = new tc_calendar("date2");
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date('d'), date('m'), date('Y'));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->dateAllow('2008-05-13', '2015-03-01', false);
$myCalendar->startMonday(true);
$myCalendar->autoSubmit(true, "form1", "test.php");
$myCalendar->autoSubmit(true, "form1");
$myCalendar->showWeeks(true);
$myCalendar->writeScript();
The thing is i cannot pass the date value from datetimepicker into form action where the url is "test.php?=date="
How would this be possible to pass the value of datetimepicker by just clicking on calendar, not using any extra button and using onsubmit event.
After execution the result in URL looks like this:
test.php?date=javascript:this.form.date2.value
Thanks
You should not need javascript to submit a form without ajax.
As you want to sent GET variables with ? sign, jsut use GET method in your form and set the name of the input to the name of the variable you want to get sent. (if you can replace 'date2' with 'date'):
<form name="form1" method="get" action="test.php">
<input name='date' />

How can I pass parameters to a new page from Javascript?

I have the following code:
$('#CreateButton').click(function (event) {
var datastoreValue = $("#SelectedDatastore").val();
window.location = "/Q/Create?datastore=" + datastoreValue;
});
It works good but I would like to have the datastore=XYZ hidden from my user. Is there another way that I can pass this information so I would be able to read it in my MVC controller. Currently I read it like this:
public ActionResult Create(string datastore)
{
When inside this method the value of datastore is available to me.
Surround you code with a form tag with a method set to POST. Also add HttpPost attribute above your Create action. This will post the data to your action without client seeing the query string value. If you are posting to another action that specify that in your action attribute inside form. For example,
<% using (Html.BeginForm("Create", "MyController", null, FormMethod.Post)) { %>
<%: Html.Label("Data store value: ")%>
<%: Html.TextBox("datastore") %>
<input type="submit" value="Submit" />
<% } %>
Your Create action will pick this up and do the rest.
Hope this helps,
Huske
You could put the datastore value in a cookie to hide it from the user, though cookies are best used for settings that you want to last awhile, not settings that are meant just for one page.
Or, you could use a Post and put the create information in a hidden form and submit that.
Or, you could use an Ajax call to issue the create and then change the page yourself afterwards.
Otherwise, the query parameter like you are using is a standard way of specifying page-level parameters for a new page.
If you stay on the same website, you can post your data to your controller; it will pick up the data from the post collection and your url won't display the query string parameters.
Just wrap your form with the control whose id is SelectedDataStore in a tag with the action set to your url and the method set to post
Here is one quick example with a submit button:
<FORM action="/Q/Create" method="post">
<INPUT id="SelectedDataStore" />
<INPUT type="submit" value="Send">
</FORM>

Categories

Resources