Modify HTML using JavaScript with an input field

On a PHP-HTML page, I am looking to dynamically change the position of an image with PHP. I know how to do this, but I also want users to have a preview before submitting the values.

<form class="ysize" action="phpchangesize.php" method="get" name="zs"><span style="color:#FF0000"><strong>-<input name="chsize" size="1" type="text" value="400" />px <input name="sb" type="submit" value="Change Cover Top" /></strong></span></form>
<style>
.ysize
{
position:absolute;
right:0px;
top:0px;
}
.cover
{
position:absolute;
height:100%;
width:100%;
left:0px;
top:-400px;
z-index:-1;
}
</style>
<img class="cover" alt="" src="<?php echo $profilecover ?>" />

When the user clicks the submit button, the PHP will write the 400 value and display it on the page. However, I do not want the user to reload the page for every attempt at changing the value. Instead, I want to create a live preview using Javascript, allowing users to see how the image will look before submitting their changes.

So, the JavaScript needs to detect the form value and update the CSS style in real-time to show users how the image will appear with the new position before they submit it.

Answer №1

To prevent page reloading every time a user submits the form, you will need to implement Ajax in your PHP script. Include an event listener for the submit event on the form element. Inside this event handler, make the Ajax call to your PHP script and update the attributes of the img tag accordingly. Don't forget to include either return false; or event.preventDefault() to stop the form from actually submitting and causing the page to reload. Best of luck.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Finding the value within a specified range of two numbers in Vue.js

When my code generates a result of 0, I need to determine the corresponding value based on the basic_salary. For example, if the basic_salary is 40,000, it should return the value of "ee": 400. <table class="table table-hover table-borde ...

Elevate the size of the hero section

Is there a way to increase the height of my jumbotron without adding it in the class attribute, as it would affect responsiveness? I want it to look like this, but currently, it appears as this. Below is my code: Html: <div class="container"> ...

"Exploring Angular 9: A guide to retrieving form data with an array of objects [Revised as of July 29th, 2020

I am encountering an issue with my Angular 9 form code. I am getting the error "ERROR TypeError: Cannot read property 'mobile_number' of undefined" and I need help in resolving this problem. <form (ngSubmit)="processForm()"> & ...

What procedures should I follow to transition from my pygame client to a browser-based game?

After developing a game server in Python to connect with multiple clients using Pygame via sockets, I realized that the turn-based card game I created doesn't require a lot of data flow since it's in JSON format. To avoid the need for clients to ...

Having issues with using setTimeOut within a for loop in JavaScript React

I'm currently working on a visual sorting algorithm application using React. My implementation involves bubble sort, where I generate an array of numbers from 1 to 50, shuffle them, and then apply the bubble sort method to sort them. However, I am fac ...

Issue displaying Highcharts graph within ASP.Net MVC framework

While attempting to bind data to the Highcharts library and display a column chart on a webpage, I used the following jQuery AJAX method to call the controller's action: <script type="text/javascript"> $(document).ready(function () { $.ajax ...

Discovering documents using the outcome of a function in mongoose

Hey there, I have a scenario with two schemas known as user and driver, both containing latitude and longitude attributes. My goal is to search the database for nearby drivers based on the user's current location (latitude and longitude) using a custo ...

No traces of SOI could be detected in the stored canvas image

I have a unique project using Three.js, where I am enabling users to save diagrams they draw on a canvas as JPEG images. The process involves: <a id="download" download="PathPlanner.jpg">Download as image</a> function download() { var dt = ca ...

Adding HTML after the last item in an ng-repeat directive in AngularJS

After incorporating the Instagram API to generate a Load More Button for displaying recent media, I am facing an issue where it overlaps with the ng-repeat and fails to append a new line after the last ng-repeat. I would be grateful for any assistance. Th ...

How to Create a Compact JavaFX ComboBox

I'm attempting to create a more compact version of the ComboBox, but no matter what I try, the space between the text and arrow button remains consistent. When using the following CSS: .combo-box-base > *.arrow-button { -fx-padding: 0 0 0 0; ...

Incorporating personalized jQuery (modal window) into a WordPress site

I created a unique jQuery modal/pop-up and now I am looking to integrate it into my WordPress site. HTML <div id="customDialog"> <form id="myForm" method="post"> <br><br><br> <p id=title name=title><b>S ...

Position the image between two div containers in the center of the screen, ensuring it is responsive and does not overlap with any text

I've been working on trying to position an image between two divs on my webpage. So far, I've managed to place the image there but it's not responsive (only displays correctly at a width of 1920) and ends up overlapping the text in both divs ...

Implementing a Push System without using node.JS

I am looking to develop a notification system similar to Facebook's, where notifications appear on the bottom-left side of the screen when someone interacts with your posts, for example. However, my challenge is that I need the server to send real-ti ...

Storing various text inputs in a MySQL database

Could anyone please assist me with fixing an issue I'm having with inserting data into a database from the form provided below? Unfortunately, I am unable to get it to work as expected. Here is the complete form: <html> <head> <m ...

Setting up multiple RabbitMQ or other backend servers within Node configurations

As someone working in DevOps, I have encountered a situation where our developers are claiming that the Node.js software they wrote can only point to a single backend server due to Node.js limitations. This assertion seems unbelievable to me. How is it eve ...

What advantages does withStyles offer that surpasses makeStyles?

Is there a distinct purpose for each? At what point is it more suitable to utilize withStyles instead of makeStyles? ...

Guide to Performing Integration Tests on (AngularJS) Web Applications

I'm currently in the process of developing a Webapp that consists of two main parts: a node rest server and an angularjs client. The structure of the app is as follows: Rest Server <--> Api Module <--> Angular App At the moment, the serv ...

The method JSON.stringify is not properly converting the entire object to a string

JSON.stringify(this.workout) is not properly stringifying the entire object. The workout variable is an instance of the Workout class, defined as follows: export class Workout { id: string; name: string; exercises: Exercise[]; routine: Ro ...

Why does the for loop assign the last iteration of jQuery onclick to all elements?

I've encountered an issue with my code that I'd like to discuss var btns = $('.gotobtn'); $('#'+btns.get(0).id).click(function() { document.querySelector('#navigator').pushPage('directions.html', myInf ...

Difficulty in defining the header while using the submit hook in Remix 1.15

When I utilize the useSubmit hook to send a POST request, it appears that the header I specify is not being taken into account. Below is the code snippet for my submit function: submit('/dashboard/', { method: 'post', heade ...