i need help whit inputs in pysript - javascript

but does anyone know how to make an input work as such, for example I am making a chatbot with this pyscript method and I have this simple problem, I cannot make the entered text be saved in the variable to do this process, I am new to this and it would help me, here in the python code fragment you can see a def with the name get_response(user_input) that method is the one that is in charge of generating everything but it gives me an error when executing it since the "user_input" does not has no data entered is the only error I have and I don't know how to fix it so I can read it in html
def get_response(user_input):
split_message = re.split(r'\s|[,:;.?!-_]\s*', user_input.lower())
response = check_all_messages(split_message)
return response

It has document element selectors, you can normally use an input(text).
Use the following as:
<input type=“text” id=“my-txt-field” />
.
.
.
user_input = Element('my-txt-field').element.value;
print(user_input)

Related

Responding to jQuery Ajax request with Python

I am currently trying to implement the pre-built inline editor located here: https://github.com/wbotelhos/inplace
Unfortunately, the support documentation leaves a lot to desire and I have very little experience with Javascript, jQuery, or Ajax.
I have been able to successfully implement the HTML edits:
<td><div class="inplace" data-field-name="name" data-field-value="{{people['name']}}" data-url="/update/{{id}}">{{ people['name'] }}</a></td>
The Js:
<script type="text/javascript">
$('.inplace').inplace();
</script>
and have successfully grabbed, and printed the info sent from the Javascript.
#app.route('/update/<id>', methods=["POST", "PATCH"])
#login_required
def update(id):
new_data = request.get_data(as_text=True)
print(new_data)
return "200"
The issue I am facing, is that the Js returns an Undefined value which is what the HTML updates to.
Ignore the return "200" - I have tired several different methods. Success = True, json values, etc and nothing seems to work.
I am sure I am missing something simple.
It looks like you need to print json with the field name that matches your field_name attribute which is name.
So you will need to print something like this. I don't use python, so you will need to follow actual python syntax. Where the word name is correct, but you will need to add the value that you want shown
print('{"name":"NEW FIELD VALUE"}')

Get Id from a value in a controller in Symfony

What I am trying to do is to get an Id from a folder number.
Both my Id and my folder number (folder number is unique) are in the same controller.
I get the folder number from a text input and I then need to redirect the user to a page /Id.
My question is how should I handle it ?
Do I need to create a method getIdFromFolderNumber() in my controller and then call this function in my JS function ?
Or maybe do I need to do everything in a JS function (I know I need a JS function since I will use an AJAX request to redirect my user and to get the value from the input).
I don't need the code or anything, I just want to know the method I need to use to understand how to do it. I've started working on a very big project and since I am a junior developer I am a bit lost.
Thank you for your help.
You don't need any JS to achieve that. It's a simple form and redirection.
Once your form which gives you the number is submitted :
$number = $form->get('number')->getData();
Then you got your criteria for your doctrine request :
$id = $yourEntityRepository->findBy(['number' => $number)])->getId();
Now you have your ID for the redirection :
return $this->redirectToRoute('entity_show', ['id' => $id]);
IS that what you were looking for ?
Note : you don't need to store $number and $id as variables. It was only to make it clear. You can replace them by the actual requests where it's needed.

google script to add information to sheet , script and bootstrap form using showModalDialog

I already have the html form made from bootstrap, however, when I am trying to use the click button to add the information to google sheet from the html form it is not working
this is my GS function
function addRow(rowData){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("STUDENTS");
ws.appendRow([rowData.names,rowData.phones,rowData.idnumber,rowData.fivehr]);
and this is my function on html file
function afterButtonClick(){
var name = document.getElementById("studentnames");
var phone = document.getElementById("phonenumber");
var idnumber = document.getElementById("ID-number");
var fivehr = document.getElementById("fivehr");
var rowData = {name: name.value,phone: phone.value, idnumber: idnumber.value, fivehr: fivehr.value};
google.script.run.addRow(rowData);
}
document.getElementById("ADD").addEvenListener("click",afterButtonClick);
example for the line for the variable
<input style="font-weight:bold" type="text" class="form-control" placeholder="Student Name" aria-label="Name" id="studentnames">
the html form is perfect the way I want it, but the function on html is not adding the data to sheet
I am following a youtuber but it is not working at all, I did some changes on the wording but I guess I am missing something, if anyone can look at it and tell me my mistake, I appreciate the help thank you
if fivehr is a datetime then that is a restricted parameter and cannot be passed using google.script.run. If it is a restricted parameter then the entire object will be nulled out. Read about it below:
https://developers.google.com/apps-script/guides/html/communication#parameters_and_return_values
Keys of your object should be same.
You've to use rowData.name and rowData.phone.
ws.appendRow([rowData.name,rowData.phone,rowData.idnumber,rowData.fivehr]);
In order to see what is the problem, you can firstly go to DevTools > Console and see if there is an error message in your client side functions. If not you go to Script Editor > Executions and see if your server side functions returned an exception.

Simple MEAN Stack issue

I have a express route;
router.get('/uni/:queryst', function (req, res) {
var choice = req.params.queryst);}
Where basically I use the queryst value from the URL to use it to query something in my database. On the front end, I need to have an input field where the user types the choice and hits submit and then it translates to something like /uni/valuehere on the browser and sending GET to express where it gets caught by the router I wrote above. I don't know how can I make the input field of HTML turn into a URL of the format /uni/valuehere .
As for testing the code initially I was directly typing the URL manually with the value I intended to verify to check if the data passed. Well, users are not going to type URL's directly to search, they will need a form and that is what I can't get my head around to how to turn the input field into a URL in a format /uni/WhateverTheUserHasTypedInTheFieldShouldAppearHere
Thanks in advance for the help. I am self learning the MEAN stack and have come a long way. I need support of experienced developers as I am currently stuck here.
While sending the request write (in controller):
$http.get('/uni/'+queryst).success(function(response){
// Write code using response
});
While on the server side write:
$http.get('/uni/:queryst',function(req,res){
var choice= req.params.queryst
// Code ....
});

Pass data to database using javascript Onclick

I am a real noob when it comes to javascript/ajax, so any help will be very appreciated.
In reference to this question:
Updating a MySql database using PHP via an onClick javascript function
But mainly concerned with the answer left by Phill Sacre. I am wondering if someone could elaborate on how we are(if we can?) passing values/data through his example, using jquery.
The code example left by him is as follows:
function updateScore(answer, correct) {
if (answer == correct) {
$.post('updatescore.php');
}
}
...
<a onclick="updateScore(this, correct)" ...> </a>
Say for example, we are wanting to pass any number of values to the database with php, could someone give me a snippet example of what is required in the javascript function? Or elaborate on what is posted above please?
Thanks again all.
The simplest example I can think of is this. Make your AJAX call in your if block like this:
$.get('updatescore.php', {'score': '222'}, function(d) {
alert('Hello from PHP: ' + d);
});
On your "updatescore.php" script, just do that: update the score. And return a plain text stating wether the update operation was successful or not.
Good luck.
P.S.: You could also use POST instead of GET.
What you would do is on the php server side have a page lets say its update.php. This page will be visited by your javascript in an Ajax request, take the request and put it in a database.
The php might look something like this:
<?php
mysql_connect(...)
mysql_query("INSERT INTO table
(score) VALUES('$_GET["score"]') ")
Your javascript would simply preform an ajax request on update.php and send it the variables as get value "score".
Phil is not passing any values to the script. He's simply sending a request to the script which most likely contains logic to 'update' the score. A savvy person taking his test though could simply look at the HTML source and see the answer by checking to see what the anchor is doing.
To further nitpick about his solution, a set of radio buttons should be used, and within the form, a button or some sort of clickable element should be used to send the values to the server via an ajax request, and the values sent to the server can be analyzed and the status of the answer sent back to the page.
Since you're using jQuery, the code can be made unobtrusive as seen in the following example:
$('#submit_answer').click(function() {
var answer = 'blah' // With blah being the value of the radio button
$.get('updatescore.php',
{'value': answer},
function(d) {
alert('Your answer is: ' + d') // Where d is the string 'incorrect' or 'correct'
}
});
Enjoy.

Categories